【问题标题】:How to find control bounded in item template on master page from content page?如何从内容页面中找到母版页上项目模板中的控件?
【发布时间】:2013-08-15 02:46:21
【问题描述】:

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

<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();

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

【问题讨论】:

    标签: asp.net c#-4.0 master-pages


    【解决方案1】:

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

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

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

    if(hiddensubcategory2id != null)
    {
        // Do something with the control you found
    }
    

    【讨论】:

    • 意味着我应该首先在母版页上找到隐藏字段并将其值存储在母版页上的变量中,然后在内容页上调用该变量值
    猜你喜欢
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2010-09-27
    相关资源
    最近更新 更多