2

我在母版页上有一个嵌套的转发器。以及其项目模板中的隐藏字段。我想要内容页面上隐藏字段的值。像这样

<ul class="categories">
                        <li>
                            <div id='cssmenu'>
                                <h4>Categories</h4>
                                <asp:Repeater ID="repcategory" runat="server" OnItemDataBound="repcategory_ItemDataBound">
                                    <HeaderTemplate>
                                        <ul>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:HiddenField ID="hf1" Value='<%# Eval("CategoryID") %>' runat="server" />
                                        <li class="active has-sub">
                                            <a href='#'><span>
                                                <%#Eval("CategoryName") %></span></a>
                                            <asp:Repeater ID="repsubcategory" OnItemDataBound="repsubcategory_ItemDataBound" runat="server">
                                                <HeaderTemplate>
                                                    <ul>
                                                </HeaderTemplate>
                                                <ItemTemplate>
                                                    <asp:HiddenField ID="hf2" Value='<%# Eval("SubCategoryID") %>' runat="server" />
                                                    <li class="has-sub">
                                                        <a href='#'><span>
                                                            <%#Eval("SubCategoryName") %></span></a>
                                                        <asp:Repeater ID="repsubcategory2" runat="server">
                                                            <HeaderTemplate>
                                                                <ul>
                                                            </HeaderTemplate>
                                                            <ItemTemplate>
                                                                <asp:HiddenField ID="hf3" Value='<%# Eval("SubCategory2ID") %>' runat="server" />
                                                                <li>
                                                                    <a href="ClientProductSubCategory2.aspx"><span>
                                                                        <%#Eval("SubCategory2Name") %></span></a>
                                                                </li>
                                                            </ItemTemplate>
                                                            <FooterTemplate>
                                                                </ul>
                                                            </FooterTemplate>
                                                        </asp:Repeater>
                                                    </li>
                                                </ItemTemplate>
                                                <FooterTemplate></ul></FooterTemplate>
                                            </asp:Repeater>
                                        </li>
                                    </ItemTemplate>
                                    <FooterTemplate></ul></FooterTemplate>
                                </asp:Repeater>
                            </div>

我想要内容页面上 subcategory2id 的值并像这样编码。

 HiddenField hiddensubcategory2id = (HiddenField)Master.FindControl("hf3");
        DataSet ds = new ClientProductView().GetAllProductSubCategory2(hiddensubcategory2id.Value);
        repContent.DataSource = ds;
        repContent.DataBind();

但这是返回一个空值。请帮我解决这个问题

4

1 回答 1

1

尝试在中继器对象本身中找到控件,如下所示:

var hiddensubcategory2id = repsubcategory2.FindControl("hf3") as HiddenField;

注意:您应该始终检查 a 的结果是否FindControl()为空,如下所示:

if(hiddensubcategory2id != null)
{
    // Do something with the control you found
}
于 2013-08-15T02:52:47.933 回答