asp.net:如何在后端找到转发器div?

时间:2011-08-24 15:34:09

标签: asp.net repeater

我使用repeater来显示从数据库中获取的记录,特别是我使用div标签来显示存储在datatable中的html内容。但我不知道如何在后端找到div。在Repeater_ItemDataBound事件中,我可以使用

Control divControl=e.Item.FindControl("divControl"); 

获取它但它没有innerHtml属性来设置html内容。有谁知道如何将它作为div控件?

2 个答案:

答案 0 :(得分:7)

HtmlGenericControl定义未由特定.NET Framework类表示的所有HTML服务器控件元素的方法,属性和事件。

因此,如果它是服务器控件,您只需使用:

HtmlGenericControl divControl = e.Item.FindControl("divControl") as HtmlGenericControl;

答案 1 :(得分:1)

要在后面的代码中找到DIV,您需要将runat="server"标记添加到DIV。

如果您打算这样做,您也可以使用Panel,因为它无论如何都会输出DIV。

完成上述操作后,你应该能够找到它:

Panel panel = (Panel)Repeater1.Items[0].FindControl("Panel1");