I am trying to iterate over the contents of a Repeater containing an html table, and use the contents of each cell. This works fine for standard HTML components, but blows up when I try to use a control. The code below will print the value of the first cell, "Item #1", but will throw an HttpException when trying to access .InnerText of the second cell. The error is as follows:
Cannot get inner content of because the contents are not literal.
I have tried to use RenderControl via this solution found elsewhere, which failed for two reasons; 1) it rendered the entire HTML of the first cell and 2) it still blew up when trying to access the second cell with the following message:
'LinkButton1' of type 'LinkButton' must be placed inside
a form tag with runat=server
Is there an easy way to get the LinkButton text I'm after? Repeater code and C# can be found below.
The repeater code:
<asp:Repeater ID="Rep1" runat="server">
<HeaderTemplate>
<table id="Table1" class="data">
</HeaderTemplate>
<ItemTemplate>
<tr id="Row" runat="server">
<td>Item #1</td>
<td><asp:LinkButton ID="LinkButton1" OnClick="DoSomething"
Text="Item #2" runat="server" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
The C#:
foreach (RepeaterItem item in Rep1.Items)
{
HtmlTableRow row = item.Controls[0].FindControl("Row") as HtmlTableRow;
foreach (HtmlTableCell cell in row.Cells)
{
if (cell.InnerText != string.Empty)
{
Console.WriteLine(cell.InnerText);
}
}
}
For anyone else who arrives here:
You cannot get InnerHtml
or InnerText
on a Control
unless its contents are literal, i.e. there are not any server controls or controls with runat="server"
inside it that required rendering
RenderControl C#
StringWriter iSW = new StringWriter();
HtmlTextWriter iHTW = new HtmlTextWriter(iSW);
iDiv.RenderControl(iHTW);
string iS = iSW.GetStringBuilder().ToString();
Include
public override void VerifyRenderingInServerForm(Control control)
{
return;
}
RenderControl VB
Dim iSW As New StringWriter
Dim iHTW As New HtmlTextWriter(iSW)
iDiv.RenderControl(iHTW)
Dim iS As String = iSW.GetStringBuilder().ToString()
Include
Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
Return
End Sub
Iterate/recurse through any child controls in the Controls collection. When you get to a leaf node (no children), then access that control's InnerText
property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With