首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在母版页中的无序列表中找到一个中继器?

在母版页中的无序列表中找到一个中继器?
EN

Stack Overflow用户
提问于 2013-08-07 12:45:07
回答 1查看 1.5K关注 0票数 2

我已将html模板加载到母版页中,然后将类别绑定到数据库中。我使用过这种编码。

代码语言:javascript
运行
复制
<ul class="categories">
            <li id="categoryItem">
                <h4>Categories</h4>
                <ul class="categories"  id="categorylist">            
                    <asp:Repeater ID="repCategories" runat="server">
                        <HeaderTemplate><ul></HeaderTemplate>
                        <ItemTemplate>
                            <li>
                            <asp:HyperLink ID="hyperCategories" runat="server"><%#Eval("CategoryName")%></asp:HyperLink>
                            </li>
                        </ItemTemplate>
                        <FooterTemplate></ul></FooterTemplate>
                    </asp:Repeater>
                </ul>
            </li>

并尝试通过在master.cs页面上进行编码将此中继器绑定到我的数据库。

代码语言:javascript
运行
复制
 if (!IsPostBack)
        {
            DataSet ds = new ViewAction().GetAllProductCategoryData();
            repCategories.DataSource = ds;
            repCategories.DataBind();
        }

但它显示了一个错误

"The name repCategories does not exist in the current context"

为什么它会显示这个错误帮助我解决这个问题。请

EN

Stack Overflow用户

回答已采纳

发布于 2013-08-07 13:02:31

您的代码(编写的)无法工作的原因是,Repeater嵌套在其他两个服务器控件中:

  • <li runat="server">
  • <ul class="categories" runat="server" id="categorylist">

这意味着Repeater与顶级元素处于不同的“命名容器”中,并且无法从您的主页代码隐藏文件中直接访问。

要解决这个问题,你需要

  1. 从这些控件中删除runat="server" (如果您实际上不需要从服务器端代码访问它们)。这将允许您的代码以现在的方式工作。或,
  2. <li>元素添加一个ID,然后使用FindControl方法访问嵌套的Repeater。

选项二将类似于这样(假设您给了<li>一个ID为"categoryItem"):

代码语言:javascript
运行
复制
if (!IsPostBack)
{
    // Get the Repeater from nested controls first
    Repeater repCategories = (Repeater)categoryItem.FindControl("categorylist").FindControl("repCategories");
    // Do the rest of your work
    DataSet ds = new ViewAction().GetAllProductCategoryData();
    repCategories.DataSource = ds;
    repCategories.DataBind();
}

您需要使用该代码在任何需要在代码背后访问它的地方“获取”Repeater。

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18103739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档