DropDownList not maintaining ViewState for "User Mode Menu" Selector
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.S开发者_运维知识库electedValue = 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;
}
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.
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.
I'm guessing you're trying to create cascading DDLs? If so, you need to overrride LoadViewState and SaveViewState and manipulate your ViewState there :)
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.
精彩评论