27

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);
        }
    }
}
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
cqx2234
  • 271
  • 1
  • 3
  • 4
  • Were you able to fix this please? I am in desperate need of an answer, as I get the same errors as you. I've even tried using System.Windows.Forms controls to get around this, but no joy. – Fandango68 May 11 '15 at 07:09

5 Answers5

28

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

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
katbyte
  • 2,665
  • 2
  • 28
  • 19
  • 1
    Could you please give a more specific example to fix the above problem? I am having the exact same issue. Thank you – Fandango68 May 11 '15 at 07:08
  • 2
    You cannot fix it without redesigning the control so its contents are literal, remove any sub controls with runat="server" – katbyte Jul 16 '15 at 21:24
  • 2
    Also, apparently an HTML comment gets parsed as "not a literal control", even if it's like `` instead of `<%-- Comment --%>`. That was what got me here. – Kristen Hammack May 17 '17 at 19:03
3

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
3

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.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
-1

Also, make sure that you do not have anything such trying to access another view control while another view control is activated in a multi view control.

user48192
  • 31
  • 4
  • 1
    Welcome to stackoverflow!! This does not provide an answer to the question. Please use comment box below and Once you have sufficient reputation you will be able to comment on any post. :) – Rucha Bhatt Joshi Jun 20 '17 at 04:29
-2

string value = ((Literal)(cell.Controls[0])).Text

Sakthi
  • 9
  • 4