简体   繁体   中英

MenuItem event wasn't triggered in asp.net menu

The menu item click event was failed to trigger code behind event method when click the menu item. I added the OnMenuItemClick(Menu_Item_Click) at menu even though it was getting failed to trigger the code behind event.

How can i sort it out this issue?

/** Asp.net Template code goes here **/

     <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" 
            EnableViewState="True" IncludeStyleBlock="False" Orientation="Horizontal" 
            BackColor="#F7F6F3" DynamicHorizontalOffset="2" Font-Names="Arial, Helvetica, sans-serif"
            Font-Size="0.8em" ForeColor="#7C6F57" StaticSubMenuIndent="10px" OnMenuItemClick="Menu_Item_Click">

<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx"  Text="Default">
<asp:MenuItem NavigateUrl="~/Search.aspx" Text="Search">
</Items>
</asp:Menu>

Codebehind Code Goes here

protected void Menu_Item_Click ( object sender, MenuEventArgs e )
{
   /** Some Validation goes here **/
}

I also experienced this problem. I solved it by eliminating the field NavigateUrl. If you eliminate this field then the click event will be triggered. In the function(event handler function) you can use the following code to navigate to the necessary page using the following code.

protected void MainMenu_MenuItemClick(object sender, MenuEventArgs e)
{
    /*your necessary code*/
    Response.Redirect(((Menu)sender).SelectedItem.Target);

}

In the above code TargetField is specified in the Menu tag with the necessary address.

or you can specify the address directly by.

protected void MainMenu_MenuItemClick(object sender, MenuEventArgs e)
{
    /*your necessary code*/
    Response.Redirect("Page.aspx");

}

According to your code and description ,for my experience the issue is related to the text and the value properties of menuitem are not specified.

Ex:

 <asp:Menu runat="server" ID="MainMenuCtl" BorderWidth="0"
            Orientation="Vertical" onmenuitemclick="MainMenuCtl_MenuItemClick">
            <Items>
                <asp:MenuItem ImageUrl="~\App_Themes/SiteDefault/Images/Mediabutton.png" value="&nbsp;" ToolTip="Media Clips">
                </asp:MenuItem>
                <asp:MenuItem ImageUrl="~\App_Themes/SiteDefault/Images/CalendarButton.png" value="&nbsp;&nbsp;" ToolTip="View calendar of events">
                </asp:MenuItem>
                <asp:MenuItem ImageUrl="~\App_Themes/SiteDefault/Images/PoetryButtonReg.png" value="&nbsp;&nbsp;&nbsp;" ToolTip="Poetry">
                </asp:MenuItem>
            </Items>
        </asp:Menu>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM