Repeater headertemplate find <ul> on ItemDataBound
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
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:
<ul>
<li id="listItem" runat="server">Hello!</li>
</ul>
If that doesn't give any errors, you should be able to access it in code behind like this:
HtmlGenericControl ctrl = e.Item.FindControl("listItem");
精彩评论