c# - NullReferenceException error trying to FindControl on !IsPostBack -


i have menu/multiview control setup inside formview , trying set first menu item selected on page_load. i'm getting nullreferenceexception error on line i'm trying set selected = true.

markup:

<asp:formview id="formview1" runat="server" cellpadding="4" datakeynames="projectid" datasourceid="projectdetailssql" forecolor="#333333">     <itemtemplate>         <h1><asp:label id="label1" runat="server" text='<%# eval("projectid") %>' /> - <asp:label id="label2" runat="server" text='<%# bind("projectname") %>' /></h1>         <asp:menu id="mnuproject" runat="server" cssclass="menustyle" orientation="horizontal" onmenuitemclick="mnuproject_menuitemclick" enableviewstate="false">             <staticselectedstyle backcolor="gray" borderstyle="solid" bordercolor="black" borderwidth="1"/>             <items>                 <asp:menuitem text="general" value="0" />                 <asp:menuitem text="scope" value="1" />                 <asp:menuitem text="cad" value="2" />                 <asp:menuitem text="pm" value="3" />                   <asp:menuitem text="submittals" value="4" />                 <asp:menuitem text="changeorders" value="5" />                 <asp:menuitem text="timecards" value="6" />                 <asp:menuitem text="docs" value="7" />                 <asp:menuitem text="log" value="8" />                 <asp:menuitem text="financials" value="9" />             </items>         </asp:menu>         <asp:multiview id=multiview1></asp:multiview>     </itemtemplate> </asp:formview> 

codebehind:

protected void page_load(object sender, eventargs e)         {             if (!ispostback)             {                 menu mnuproject = (menu)formview1.findcontrol("mnuproject");                 mnuproject.items[0].selected = true; <----- exception thrown here             }         } 

i have tried menu mnuproject = (menu)formview1.row.findcontrol("mnuproject"); , mnuproject still coming null. can guess i'm not giving right location findcontrol. correcting syntax appreciated.

the findcontrol method not recursive.

that means attempt find contol in item request, not children. in other words, looking mnuproject within formview1 not of formview1's child controls.

this generic implementation used resolve it. need use recursion behave way want... lucky had project open :-)

public static class pagehelpers {             public static system.web.ui.control findcontrolrecursive(system.web.ui.control root, string id)             {                 if (root.id == id)                 {                     return root;                 }                  foreach (system.web.ui.control c in root.controls)                 {                     system.web.ui.control t = pagehelpers.findcontrolrecursive(c, id);                     if (t != null)                     {                         return t;                     }                 }                 return null;             } } 

and let's refactor page code, can determine if resolved issue.

  menu mnuproject = (menu)pagehelpers.findcontrolrecursive(formview1,"mnuproject");   // lets test see if our findcontrolrecursive method worked before doing else   if(mnuproject == null) {throw new exception("findcontrolrecursive failed!");}    mnuproject.items[0].selected = true; <----- exception thrown here 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -