Asp.net 在母版页的无序列表中查找转发器?
我已将html模板加载到母版页,然后将类别绑定到数据库。 我用过这个编码Asp.net 在母版页的无序列表中查找转发器?,asp.net,master-pages,Asp.net,Master Pages,我已将html模板加载到母版页,然后将类别绑定到数据库。 我用过这个编码 <ul class="categories"> <li id="categoryItem"> <h4>Categories</h4> <ul class="categories" id="categorylist">
<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>
但它显示了一个错误
“当前上下文中不存在repCategories名称”
为什么会显示此错误?请帮助我解决此问题。请您的代码(如所写)不起作用的原因是中继器嵌套在其他两个服务器控件中:
和
李>
runat=“server”
(如果您实际上不需要从服务器端代码访问它们)。这将允许您的代码按现在的方式工作或,
元素添加一个ID,然后使用FindControl
方法访问嵌套中继器
一个“categoryItem”的ID):
您需要使用该代码在需要在codebehind中访问转发器的任何位置“获取”转发器。@VJain您还没有显示整个母版页标记。在您显示的内容之外是否还有其他
runat=“server”
控件?另外,您能否确认此C#代码位于masterpage.aspx.cs文件中?我想确保我正确地阅读了你的问题。不,先生,实际上现在它显示了你的错误categoryItem@VJain如果删除了“runat=“server”
引用,则不应运行具有categoryItem的代码。问题中的代码应该可以运行(只要没有显示更多的服务器控件).sir以上内容已修改,不再运行runat=“Server”让我们
if (!IsPostBack)
{
DataSet ds = new ViewAction().GetAllProductCategoryData();
repCategories.DataSource = ds;
repCategories.DataBind();
}
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();
}