SET SESSION VARIABLE ON MENU ITEM'S CLICK IN ASP.NET

Mark Devid

Total Post:102

Points:714
Posted by  Mark Devid
 2191  View(s)
Ratings:
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" />

  1. Kamlakar Singh

    Post:194

    Points:1396
    Re: Set Session variable on menu item's click in asp.net

    <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;

            }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!