共用方式為


TableCell.AddAttributesToRender(HtmlTextWriter) 方法

定義

TableCell 控制項的特定屬性加入要呈現的屬性清單中。

protected:
 override void AddAttributesToRender(System::Web::UI::HtmlTextWriter ^ writer);
protected override void AddAttributesToRender (System.Web.UI.HtmlTextWriter writer);
override this.AddAttributesToRender : System.Web.UI.HtmlTextWriter -> unit
Protected Overrides Sub AddAttributesToRender (writer As HtmlTextWriter)

參數

writer
HtmlTextWriter

將 HTML 內容呈現給用戶端的輸出資料流。

例外狀況

找不到列為關聯標題儲存格的儲存格。

範例

下列程式代碼範例示範如何在自定義伺服器控制項中覆寫 AddAttributesToRender 方法,讓每個儲存格轉譯自定義標題屬性。

<%@ Page Language="C#" %>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
    <title>Custom TableCell - AddAttributesToRender - C# Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h3>Custom TableCell - AddAttributesToRender - C# Example</h3>

    <asp:Table id="Table1" runat="server" 
      CellPadding="3" CellSpacing="2">
      <asp:TableRow>
        <aspSample:CustomTableCellAddAttributesToRender Text="(0,0)" />
        <aspSample:CustomTableCellAddAttributesToRender Text="(0,1)" />
        <aspSample:CustomTableCellAddAttributesToRender Text="(0,2)" />
      </asp:TableRow>
      <asp:TableRow>
        <aspSample:CustomTableCellAddAttributesToRender Text="(1,0)" />
        <aspSample:CustomTableCellAddAttributesToRender Text="(1,1)" />
        <aspSample:CustomTableCellAddAttributesToRender Text="(1,2)" />
      </asp:TableRow>
    </asp:Table>
      
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
    <title>Custom TableCell - AddAttributesToRender - VB.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h3>Custom TableCell - AddAttributesToRender - VB Example</h3>

    <asp:Table id="Table1" runat="server" 
      CellPadding="3" CellSpacing="2">
      <asp:TableRow>
        <aspSample:CustomTableCellAddAttributesToRender Text="(0,0)" />
        <aspSample:CustomTableCellAddAttributesToRender Text="(0,1)" />
        <aspSample:CustomTableCellAddAttributesToRender Text="(0,2)" />
      </asp:TableRow>
      <asp:TableRow>
        <aspSample:CustomTableCellAddAttributesToRender Text="(1,0)" />
        <aspSample:CustomTableCellAddAttributesToRender Text="(1,1)" />
        <aspSample:CustomTableCellAddAttributesToRender Text="(1,2)" />
      </asp:TableRow>
    </asp:Table>
      
    </div>
    </form>
</body>
</html>
using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
  public sealed class CustomTableCellAddAttributesToRender : System.Web.UI.WebControls.TableCell
  {
    protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
    {
      // Write a Title attribute.
      writer.AddAttribute("title", "CustomAddAttributesToRender");

      // Call the base AddAttributesToRender method.
      base.AddAttributesToRender(writer);
    }
  }
}
Imports System.Web
Imports System.Security.Permissions

Namespace Samples.AspNet.VB.Controls
    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public NotInheritable Class CustomTableCellAddAttributesToRender
        Inherits System.Web.UI.WebControls.TableCell

        Protected Overrides Sub AddAttributesToRender(ByVal writer As System.Web.UI.HtmlTextWriter)

            ' Write a Title attribute.
            writer.AddAttribute("title", "CustomAddAttributesToRender")

            ' Call the base AddAttributesToRender method.
            MyBase.AddAttributesToRender(writer)
        End Sub
    End Class
End Namespace

備註

方法 AddAttributesToRender 會覆寫基類的 AddAttributesToRender 方法,並先呼叫基底方法,然後加入 TableCell 對應至 RowSpan 屬性、 ColumnSpan 屬性和 AssociatedHeaderCellID 屬性的屬性。 AssociatedHeaderCellID如果屬性參考TableHeaderCell找不到的 ,HttpException則會擲回 。

方法 AddAttributesToRender 主要是由控件開發人員用來擴充控件的功能 TableCell

給繼承者的注意事項

AddAttributesToRender(HtmlTextWriter) 衍生類別中覆寫 方法時,請務必呼叫 AddAttributesToRender(HtmlTextWriter) 基類的方法, TableCell 以便正確轉譯控件屬性。

適用於

另請參閱