I would like to create a user control which contains the following in the ASCX file:
...
<tr runat="server" ID="mytr">
<td runat="server" ID="myCell1"></td>
<asp:PlaceHolder runat="server" ID="myPlaceholder"></asp:PlaceHolder>
<td runat="server" ID="myCell2"></td>
</tr>
...
This generates the following error:
System.Web.UI.HtmlControls.HtmlTableCellCollection must have items of
type ‘System.Web.UI.HtmlControls.HtmlTableCell’. ‘asp:PlaceHolder’ is
of type ‘System.Web.UI.WebControls.PlaceHolder’.
The reason for the PlaceHolder is I would like to dynamically generate that cell (for example choosing between TD and TH, or choosing whether to create it at all.
Is there an easy way to do this? Or maybe I should just create my own custom control which inherits HtmlTableCell?
I have noticed that viewstate for certain table elements are keyed by the ordinal (index into child control collection), which means I also sometimes cannot dynamically generate cells when I would expect to be able to. Is there some general best practice with regards to this?
I have also considered always creating the TD, but just hiding it. The problem is that I use rowspan on a previous row, meaning different rows will render different numbers of cells. This is conditional and can change between postbacks (by enabling / disabling a row).
I managed to solve it by making a simple custom control:
And now the ascx is: