转发器标题模板查找;在 ItemDataBound 上
我正在尝试向中继器 HeaderTemplate 中的 ItemDataBound 上 ID 为“SubNav2”的标签添加属性。
但我不断收到错误:对象引用未设置到对象的实例。
我认为这是因为它没有找到 ID 为“SubNav2”的对象,我是否以正确的方式处理它?
附加代码背后
If e.Item.ItemType = ListItemType.Header Then
Dim ulSubNav2 As HtmlGenericControl = CType(e.Item.FindControl("SubNav2"), HtmlGenericControl)
ulSubNav2.Style.Add("display", "block")
End If
的代码 所以我试图找到的是第二个中继器 ID=reSubNav2。它必须有 runat=server 吗?
<asp:Repeater ID="reSubNav" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><asp:HyperLink ID="SubNavLink" runat="server"></asp:HyperLink>
<asp:Repeater ID="reSubNav2" runat="server" OnItemDataBound="reSubNav2_ItemDataBound">
<HeaderTemplate>
<ul id="SubNav2" style="display:none;">
</HeaderTemplate>
<ItemTemplate>
<li><asp:HyperLink ID="SubNavLink2" runat="server"></asp:HyperLink></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
<ul>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
背后的代码
Protected Sub reSubNav2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim rowView As System.Data.DataRowView
rowView = CType(e.Item.DataItem, System.Data.DataRowView)
ElseIf e.Item.ItemType = ListItemType.Header Then
Dim ulSubNav2 As HtmlGenericControl = CType(e.Item.FindControl("SubNav2"), HtmlGenericControl)
ulSubNav2.Style.Add("display", "block")
End If
End Sub
I am trying to add an attribute to a tag with ID 'SubNav2' on ItemDataBound which is in the HeaderTemplate of a repeater.
But I keep getting the error: Object reference not set to an instance of an object.
Which i think is because it is not finding the object with ID 'SubNav2', am i going about it the correct way?
CODE BEHIND
If e.Item.ItemType = ListItemType.Header Then
Dim ulSubNav2 As HtmlGenericControl = CType(e.Item.FindControl("SubNav2"), HtmlGenericControl)
ulSubNav2.Style.Add("display", "block")
End If
Additonal Code
So the i'm trying to find is in the second repeater ID=reSubNav2. Does it have to have runat=server on it?
<asp:Repeater ID="reSubNav" runat="server">
<HeaderTemplate>
<ul>
</HeaderTemplate>
<ItemTemplate>
<li><asp:HyperLink ID="SubNavLink" runat="server"></asp:HyperLink>
<asp:Repeater ID="reSubNav2" runat="server" OnItemDataBound="reSubNav2_ItemDataBound">
<HeaderTemplate>
<ul id="SubNav2" style="display:none;">
</HeaderTemplate>
<ItemTemplate>
<li><asp:HyperLink ID="SubNavLink2" runat="server"></asp:HyperLink></li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
<ul>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
CODE BEHIND
Protected Sub reSubNav2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim rowView As System.Data.DataRowView
rowView = CType(e.Item.DataItem, System.Data.DataRowView)
ElseIf e.Item.ItemType = ListItemType.Header Then
Dim ulSubNav2 As HtmlGenericControl = CType(e.Item.FindControl("SubNav2"), HtmlGenericControl)
ulSubNav2.Style.Add("display", "block")
End If
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保 UL 具有 runat="server" 标记,您应该能够找到它。
编辑
不要在服务器上运行整个列表,而是尝试在服务器上运行列表项,如下所示:
如果没有给出任何错误,您应该能够在后面的代码中访问它像这样:
Make sure that the UL has the runat="server" tag, and you should be able to find it.
EDIT
Instead of running the whole list at the server, try just running the list item at the server, like this:
If that doesn't give any errors, you should be able to access it in code behind like this: