0

I'll keep this simple. For development purposes I want to create "modes" representing various user types which will see a different menu bar accordingly. I've got everything the way I want it except after selecting the "mode" in which I wish to operate, as soon as I click on a menu item, it forgets which "mode" it was in and refreshes with the default menu bar. I'm assuming this is an issue with ViewState not maintaining properly over the postback? I've searched all over the forums and elsewhere and can't figure out how to accomplish what I want. Here is my code with codebehind.

I added the following to Page_Load:

If Not IsPostBack Then
    modeMenu.SelectedValue = Session("mode")
End If

EDIT: The rest is the same with the exception of one additional line in the code behind. Thanks to TC for the answer :)

<asp:Menu ID="mode1Nav" runat="server" CssClass="menu" 
    Visible="False" ViewStateMode="Enabled">
    <Items>
        <asp:MenuItem NavigateUrl="~/Home1.aspx" />
        <asp:MenuItem NavigateUrl="~/DoSomething1.aspx" />
    </Items>
</asp:Menu>
<asp:Menu ID="mode2Nav" runat="server" CssClass="menu" 
    Visible="False">
    <Items>
        <asp:MenuItem NavigateUrl="~/Home2.aspx" />
        <asp:MenuItem NavigateUrl="~/DoSomething2.aspx" />
    </Items>
</asp:Menu>

This "modeMenu" is used to select my mode.

<asp:DropDownList ID="modeMenu" runat="server" AutoPostBack="True" 
    ViewStateMode="Enabled" CssClass="modeMenu">
    <asp:ListItem>Mode1</asp:ListItem>
    <asp:ListItem>Mode2</asp:ListItem>
</asp:DropDownList>

This code behind Sub is intended to display the appropriate menu for whatever mode your in, AND REMEMBER IT, lol.

Protected Sub mode(ByVal sender As Object, ByVal e As System.EventArgs) Handles modeMenu.Load, modeMenu.SelectedIndexChanged
    Session.Add("mode", modeMenu.SelectedValue)  //Added this
    Select Case (modeMenu.SelectedValue)
        Case "Mode1"
            mode1Nav.Visible = True
            mode2Nav.Visible = False
        Case "Mode2"
            mode1Nav.Visible = False
            mode2Nav.Visible = True
    End Select
End Sub

Like I said, the problem is that after selecting my mode, once I try to use the menu, it refreshes and forgets the mode.

EDIT: Additionally I added the following CSS to put my mode menu out of the way. It works quite nicely like this. Really handy for development. :)

.modeMenu {
    position: fixed;
    top: 5px;
    left: 5px;
}
Chiramisu
  • 4,687
  • 7
  • 47
  • 77

4 Answers4

0

If I understand correctly (I've never used the menu control), the problem is that clicking your menu item causes you to navigate to a new URL. ViewState only persists between PostBacks, not between "fresh" visits to new URLs. Try using Session State instead.

TC.
  • 4,133
  • 3
  • 31
  • 33
  • @TC: I was having issues getting that to work, but I'll look into it a bit more and give it another try. – Chiramisu Jul 19 '11 at 21:59
  • @TC: Ack, no wonder. I've literally been trying to use SessionState (the namespace) instead of Session (the object) doh! Thanks so much for causing me to take a second look at my blunder. Cheers :) – Chiramisu Jul 19 '11 at 22:34
0

Can you use a cookie to store your mode?

When user selects the mode, store the value in a cookie. When any page is loaded afterwards, check for cookie in page load event and set the mode appropriately.

If you are familiar with Jquery, you can use the jquery cookie plugin also, that way your page does not have to post back when cookie is created.

coder net
  • 3,447
  • 5
  • 31
  • 40
  • Nah, I prefer the mode to be maintained server side. – Chiramisu Jul 19 '11 at 21:58
  • you do realize that if you store this in session state, it is only valid for the duration of the user's visit. Cookie does remember the mode when they come back. if you want to store it server side, you can also store it in the db.. but it all depends on your requirement which you know better! – coder net Jul 20 '11 at 14:43
0

I'm guessing you're trying to create cascading DDLs? If so, you need to overrride LoadViewState and SaveViewState and manipulate your ViewState there :)

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
-1

The property Visible isn't stored in the ViewState. In order for it to be remembered, you should put something in your Page_Load() event handler.

Al W
  • 7,539
  • 1
  • 19
  • 40