Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get inner content of '' because the contents are not literal

Tags:

asp.net

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);
        }
    }
}
like image 731
cqx2234 Avatar asked Apr 09 '10 19:04

cqx2234


3 Answers

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

like image 70
katbyte Avatar answered Nov 16 '22 07:11

katbyte


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
like image 21
Cristiano Ronaldo CR7 Avatar answered Nov 16 '22 05:11

Cristiano Ronaldo CR7


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.

like image 2
Jon Adams Avatar answered Nov 16 '22 06:11

Jon Adams