0

Code bebind as on page load event:

Menu menu = (Menu)Page.Master.FindControl("NavigationMenuAdmin");
if (menu != null)
{
    MenuItemtext = menu.SelectedItem.Text;
    Response.Write("Selected Item is: " + MenuItemtext);
}

But gives error as:

Object reference not set to an instance of an object.

<asp:Menu ID="NavigationMenuAdmin" runat="server" EnableViewState="false"
IncludeStyleBlock="false" Orientation="Horizontal"
CssClass="navbar"
StaticMenuStyle-CssClass="nav"
StaticSelectedStyle-CssClass="active"
DynamicMenuStyle-CssClass="dropdown-menu">
<Items>
       <asp:MenuItem Text="Home"></asp:MenuItem>
       <asp:MenuItem Text="Candidate">
       <asp:MenuItem Text="GetList" NavigateUrl="~/LookupHandler.aspx" />
       <asp:MenuItem Text="AddNew" NavigateUrl="~/LookupHandler.aspx" />
       <asp:MenuItem Text="Update" NavigateUrl="~/LookupHandler.aspx" />
       </asp:MenuItem>
       <asp:MenuItem Text="Master">
       <asp:MenuItem Text="GetList" NavigateUrl="~/LookupHandler.aspx" />
       <asp:MenuItem Text="AddNew" NavigateUrl="~/LookupHandler.aspx" />
       </asp:MenuItem>
</Items>
</asp:Menu>
kls
  • 89
  • 2
  • 12
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Anthony Mar 24 '15 at 13:27
  • So you get into the `if` but `menu.SelectedItem` is null? – Tim Schmelter Mar 24 '15 at 13:35

2 Answers2

1

Please check that the variable named "MenuItemtext" is declared before the line.

MenuItemText = menu.SelectedItem.Text;

This can be the reason its showing you this error.

"Object reference not set to an instance of an object." - This Error is shown when a variable or object is not declared and is used at runtime.

Try This code:

protected void Page_Load(object sender, EventArgs e)
{
    Menu Menu = (Menu)Page.Master.FindControl("NavigationMenuAdmin");
    Menu.MenuItemClick +=Menu_MenuItemClick;
}

void Menu_MenuItemClick(object sender, MenuEventArgs Events)
{
    Menu Menu = (Menu)sender;
    MenuItem selectedItem = Menu.SelectedItem;
    Response.Write("Selected Item is: " + Menu.SelectedItem.Text + ".");
}

All that is required is you bind the Menu click event at PageLoad and process the clicked item in the MenuItemClick EventHandler.

Rishabh
  • 269
  • 2
  • 12
0

You need to see if the menu item is not null

MenuItem selectedItem = menu.SelectedItem;
if (selectedItem != null)
{
}
mjroodt
  • 2,833
  • 5
  • 19
  • 35
  • I get that as null but how to do that for getting selected value..! – kls Mar 24 '15 at 13:34
  • I could be wrong but you're doing this on page load so how would it know whats selected. You either have to select an item when the menu menu is being bound or hook into the menu click event and get the selected item that way – mjroodt Mar 24 '15 at 13:42
  • have a look here http://stackoverflow.com/questions/2770042/set-item-selected-in-asp-net-menu-control – mjroodt Mar 24 '15 at 13:43
  • So how I can bind event for that menu of master page on content page? – kls Mar 24 '15 at 13:44
  • and here to hook into the menu click event https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.menu.selecteditem%28v=vs.110%29.aspx – mjroodt Mar 24 '15 at 13:44
  • Menu control is on master page and I want to handle event on content page load event so how to do that? – kls Mar 24 '15 at 13:48
  • Oh thank I write code for MenuEventHandler and it works, thanks! – kls Mar 24 '15 at 14:02