I’m playing around with deriving from System.Web.UI.WebControls.Table/Cell/Row and can’t get HtmlTextWriter.AddAttribute to work:
Table:
public class Table : System.Web.UI.WebControls.Table
{
public string Title { get; set; }
public bool Sorted { get; set; }
public Table()
{
}
}
Table Header Row:
public class TableHeaderRow : System.Web.UI.WebControls.TableHeaderRow
{
public TableHeaderRow()
{
this.TableSection = TableRowSection.TableHeader;
this.ClientIDMode = System.Web.UI.ClientIDMode.Static;
}
}
Table Header Cell:
public class TableHeaderCell : System.Web.UI.WebControls.TableHeaderCell
{
public TableHeaderCell()
{
}
public override void RenderBeginTag(HtmlTextWriter writer)
{
AddAttributesToRender(writer);
writer.AddAttribute("test", "whee");
writer.WriteBeginTag("th");
//writer.WriteAttribute("test", "whee");
writer.WriteLine('>');
}
public override void RenderEndTag(HtmlTextWriter writer)
{
writer.WriteEndTag("th");
writer.WriteLine();
}
}
usage:
<Internal:Table ID="somethingTbl" runat="server" Title="A Table" Sorted="true">
<Internal:TableHeaderRow>
<Internal:TableHeaderCell>
Stuff
</Internal:TableHeaderCell>
<Internal:TableHeaderCell>
More Stuff
</Internal:TableHeaderCell>
<Internal:TableHeaderCell>
Less Stuff
</Internal:TableHeaderCell>
</Internal:TableHeaderRow>
</Internal:Table>
And then some regular rows are added in the cs. I get the following partial output:
<table id="activationsTbl">
<thead>
<tr>
<th>
Stuff
</th>
<th>
More Stuff
</th>
<th>
Less Stuff
</th>
</tr>
</thead><tbody test="whee" test="whee" test="whee">
...
As you can see, the attributes are being written out with the next opening tag after all of the th tags (the tbody tag at the end). If I use the commented out WriteAttributes then it gets written correctly. If there aren’t any other rows (and thus no <tbody>), then the attributes are not written to any other tags.
And minutes later I realize that there is a difference between WriteBeginTag and RenderBeginTag *facepalm*.