转发器标题模板查找
    ;在 ItemDataBound 上

发布于 2024-12-01 08:32:56 字数 2103 浏览 3 评论 0原文

我正在尝试向中继器 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2024-12-08 08:32:56

确保 UL 具有 runat="server" 标记,您应该能够找到它。

编辑

不要在服务器上运行整个列表,而是尝试在服务器上运行列表项,如下所示:

<ul>
   <li id="listItem" runat="server">Hello!</li>
</ul>

如果没有给出任何错误,您应该能够在后面的代码中访问它像这样:

HtmlGenericControl ctrl = e.Item.FindControl("listItem");

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文