问题描述
我试图遍历包含HTML表格直放站的内容,并使用每个单元格的内容。这工作正常标准的HTML组件,但炸毁当我尝试使用控制。下面的code将打印的第一个单元格的值,项目#1,但在尝试访问第二单元的.InnerText时将抛出HttpException。误差如下:
无法获取的,因为内容不是字面上的内部内容。
我试图通过这一解决方案在其他地方发现,其中失败的原因有两个使用RenderControl; 1)它呈现的第一个单元格的整个HTML和2),它试图与下面的消息访问第二个单元格时仍然炸毁了:
类型'的LinkButton的LinkButton1必须放在里面
与RUNAT =服务器的表单标签
有没有一种简单的方法来获取LinkButton的文字,我之后?直放站code和C#可以在下面找到。
中继code:
< ASP:直放站ID =REP1=服务器>
<&HeaderTemplate中GT;
<表ID =表1级=数据>
< / HeaderTemplate中>
<&ItemTemplate中GT;
< TR n =行=服务器>
< TD>项目#1< / TD>
< TD>< ASP:LinkButton的ID =LinkButton1的OnClick =DoSomething的
文本=项目#2=服务器/>< / TD>
< / TR>
< / ItemTemplate中>
< FooterTemplate>
< /表>
< / FooterTemplate>
C#的:
的foreach(在Rep1.Items的RepeaterItem项)
{
HtmlTableRow行= item.Controls [0] .FindControl(行)作为HtmlTableRow; 的foreach(在row.Cells HtmlTableCell细胞)
{
如果(cell.InnerText!=的String.Empty)
{
Console.WriteLine(cell.InnerText);
}
}
}
有关谁比谁这里到达:
您不能得到的innerHTML
或的InnerText
在控制
除非其内容是文字,即没有任何服务器控件或控件与 =服务器
里面,需要渲染
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
这篇关于无法获取的“内部内容”,因为内容不是文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!