Home > DeveloperSection > Forums > Set Session variable on menu item's click in asp.net
Mark Devid
Mark Devid

Total Post:102

Points:714
Posted on    October-08-2014 11:27 PM

 ASP.Net ASP.Net  ASP.NET MVC  C# 
Ratings:


 1 Reply(s)
 1443  View(s)
Rate this:
ASP newbie here, in my website I need to set a session variable when I click the menu item
How can I achieve this, I have a menu control in my master page which has a sitemap file attached to it?

<asp:Menu ID="mainMenu" runat="server" DataSourceID="siteMapSource"

    StaticDisplayLevels="10" Width="150px">

    <StaticSelectedStyle CssClass="menuNodeSelected" />

    <LevelMenuItemStyles>

        <asp:MenuItemStyle Font-Bold="True" Font-Underline="False" />

    </LevelMenuItemStyles>

    <StaticMenuItemStyle CssClass="menuNode" />

</asp:Menu>

<asp:SiteMapDataSource ID="siteMapSource" runat="server" ShowStartingNode="False" />



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    October-08-2014 11:31 PM

<asp:Menu ID="mainMenu" runat="server" DataSourceID="siteMapSource"

    StaticDisplayLevels="10" Width="150px"

    OnMenuItemClick="NavigationMenu_MenuItemClick">

    <StaticSelectedStyle CssClass="menuNodeSelected" />

    <LevelMenuItemStyles>

        <asp:MenuItemStyle Font-Bold="True" Font-Underline="False" />

    </LevelMenuItemStyles>

    <StaticMenuItemStyle CssClass="menuNode" />

</asp:Menu>

<asp:SiteMapDataSource ID="siteMapSource" runat="server" ShowStartingNode="False" />

You should set a method to be called on server side OnMenuItemClick, this will rise the event of menu click. That event is (in our case): NavigationMenu_MenuItemClick.

On Code-Behind you can do whatever you want when an menu item is selected.

void NavigationMenu_MenuItemClick(Object sender, MenuEventArgs e)

        {

            // Display the text of the menu item selected by the user.

            Message.Text = "You selected " + e.Item.Text + ".";

            Session["variable"] = e.Item.Text;

        }


Don't want to miss updates? Please click the below button!

Follow MindStick