1

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
Jammer
  • 2,330
  • 11
  • 48
  • 77
  • the code looks fine. Not sure why it can not find the SubNav2. Can you post some more code? – Bobby Aug 24 '11 at 16:10
  • Hmm...strange, posted more code above, thanks for help. – Jammer Aug 24 '11 at 16:14
  • yep just looked at it as you can not put runat="server" on the ul tag it will never be seen on server side so your can only make this work using javascript. – Bobby Aug 24 '11 at 16:36

1 Answers1

1

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");
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • I've tried that but I get an error: Error creating control - reSubNav Unexpected end of file looking for tag (the tag is in the FooterTemplate). I've added more code above. – Jammer Aug 24 '11 at 16:17
  • Oh! I see what you're doing. Is there a reason why you can't do this with just JavaScript? – James Johnson Aug 24 '11 at 16:26
  • Yeah but what i'm trying to do is change the display:none on the
      tag so need to find that. There are about 10 sections each with a hidden subnav and i want to show the correct subnav dependant on which section they have selected.
    – Jammer Aug 24 '11 at 16:39
  • would e.Item.FindControl("listItem").Parent do it?? – Jammer Aug 24 '11 at 16:41
  • @JBoom: Can you set it up so that all of the items are nested within list items? Are you trying to create an accordion menu? – James Johnson Aug 24 '11 at 16:42
  • Naa it didn't. I'm not trying to create an accordion menu, theres isn't any javascript involved, i want to eventially just show one sub menu which to acheive I need to find the
      tag. I'm sure I can do it another way without the second repeater, just produce all the html in code behind, then I can add in display:none or block on whichever
        i wish.
    – Jammer Aug 24 '11 at 16:49
  • Yea, I think you'll need to find another way, because the parser is very picky about controls running at the server, and won't let you manipulate controls the way you're lookng for. – James Johnson Aug 24 '11 at 16:54
  • Sorry we couldn't get it working. Seek me out if you have questions with your next attempt. – James Johnson Aug 24 '11 at 16:58
  • I just had a bit of an idea and it worked, rather than setting the
      tag to display or not display, i just coded it on the whole repeater, so if the user is in a certain section it will bind the data to the sub repeater else it won't....if that makes sense!?!
    – Jammer Aug 25 '11 at 11:39