I have a Menu/MultiView control setup inside a FormView and I am trying to set the first menu item as selected on Page_Load. I'm getting a NullReferenceException error on the line where I'm trying to 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 also tried Menu mnuProject = (Menu)FormView1.Row.FindControl("mnuProject");
and mnuProject is still coming back as null. I can only guess that I'm not giving it the right location for FindControl. Any help correcting my syntax would be greatly appreciated.