views:

32

answers:

2

Hi,

I created an application which has a menu where it's items are created dynamicly. The menu acts as a language menu.

<body runat="server">
    <form id="Form1" runat="server">
    <table class="TableLayout">
        <tr>
            <td class="TopNav" align="right">
                <asp:Menu runat="server" ID="LanguageMenu" Orientation="Horizontal" OnMenuItemClick="LanguageMenu_MenuItemClick">
                    <LevelMenuItemStyles>
                        <asp:MenuItemStyle CssClass="TopNavItem" />
                    </LevelMenuItemStyles>
                    <StaticHoverStyle CssClass="TopNavItemHover" />
                </asp:Menu>
            </td>
        </tr>
    ...

I use session variables to set my current language.

however if I click on the menu to change the session variable:

public void LanguageMenu_MenuItemClick(Object sender, MenuEventArgs e)
        {
            Session["language"] = e.Item.Text;
        }

The page reloads with the following code:

sportsPath = String.Format(@"{0}{1}\Sports\", xmlPath, Session["language"]);
//create LeftNavigation
            string[] sports = Directory.GetFiles(sportsPath);
            LeftNavigation.Items.Clear();
            foreach (string sport in sports)
            {
                string text = sport.Replace(sportsPath, "").Replace(".xml", "");
                MenuItem item = new MenuItem();
                item.Text = text;
                LeftNavigation.Items.Add(item);
            }

The thing is the content doesn't change, only after I click on something else.

If I skip through my code after clicking on the menuItem I can see that it passes the code and it should change, however for some reason the page needs another extra trigger to modify it's content.

I also see the page reloading so I don't understand why it's not changing immediatly. I guess I'm not understanding the asp.net logic just quite yet.

What am I doing wrong?

+1  A: 

The Page_Load is triggered before the MenuItemClick event, thus causing your Session variable to be set after your page is updated.

To solve this, you could move your code in the Page_Load to the Page_PreRender event handler, which is fired after the MenuItemClick.

Prutswonder
i moved my code to a method which i fire up on the page load and also when i click my menuitem. So basicly the solution is the same, moving it out of the page_load. Thanks for clearing that up
WtFudgE
A: 

The solution to this is to Redirect the User to the same page after updating the language preference in Session.

    public void LanguageMenu_MenuItemClick(Object sender, MenuEventArgs e) 
    { 
        Session["language"] = e.Item.Text;
        Response.Redirct("samepage.aspx");
        Response.End();
    } 
this. __curious_geek