asp.net - Find a repeater inside a unordered list inside a master page? -


i have loaded html template master page , bind category database. have used coding.

<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> 

and try bind repeater database doing coding on master.cs page.

 if (!ispostback)         {             dataset ds = new viewaction().getallproductcategorydata();             repcategories.datasource = ds;             repcategories.databind();         } 

but showing error

"the name repcategories not exist in current context"

why showing error me solve this. please

the reason code (as-written) isn't working, because repeater nested within 2 other server controls:

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

this means repeater in different "naming container" top-level elements, , isn't directly accessible masterpage's codebehind file.

to fix this, need either

  1. remove runat="server" controls (if don't need access them server side code). allow code work way now. or,
  2. add id <li> element, , use findcontrol method access nested repeater.

option 2 (we'll assume gave <li> id of "categoryitem"):

if (!ispostback) {     // repeater nested controls first     repeater repcategories = (repeater)categoryitem.findcontrol("categorylist").findcontrol("repcategories");     // rest of work     dataset ds = new viewaction().getallproductcategorydata();     repcategories.datasource = ds;     repcategories.databind(); } 

you need use code "get" repeater in place need access in codebehind.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -