开发者

session variable set by checkbox not being stored/shared from page to page

I have a listview that's showing a long list. Each item has the ability to be 'hidden' or not. I want a checkbox to either show all the list, or not to show the hidden ones. The idea is that users will hide the older items they don't want to see any more, but may want to see at some point. I want to store the value of this decision in a session variable so if the user navigates to another page, then comes back, the ShowAllCheckbox will pre-populate to what the user has previously decided. Everything is working good, except i can't get the session variable to keep. It keeps going back to False. This is what I have:

aspx page:

Show Hidden: <asp:Checkbox ID="ShowHiddenCheckbox" runat="s开发者_C百科erver" AutoPostBack="True" OnCheckedChanged="ShowHiddenCheckboxChange" />
...
<asp:ListView ...>
<!-- this list works fine, and pulls the correct records -->

aspx.vb page:

Protected Sub ShowHiddenCheckBoxChange(ByVal sender As Object, ByVal e As EventArgs)
    ' toggle the values
        Dim CheckBoxField As CheckBox = TryCast(sender, CheckBox)
        If CheckBoxField.Checked Then
            Session("ShowHiddenRotations") = False
        Else
            Session("ShowHiddenRotations") = True
        End If

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'if i navigate to another page, and come back to this one, this comes back as "False".  I don't understand how it could be reset to False.
    Response.Write( "Session: " & Session("ShowHiddenRotations") )

    'when page loads check if session variable has been set to show/hide the hidden rotations
    If Session("ShowHiddenRotations") Then

        If Session("ShowHiddenRotations") = True Then
            'update sql query on select statement to show the hidden rotations

            'update checkbox to show it as checked
            ShowHiddenCheckBox.Checked = True
        Else
            ShowHiddenCheckBox.Checked = False
        End If
    Else
        'not checked by default (ie don't show hidden)
        ShowHiddenCheckBox.Checked = False
    End If

End Sub

The Session variable always reverts back to False when i navigate to another page and come back to this one. My understanding of session variables was that they would pass their values from one page to another until the user closes the browser. Maybe there's another way of doing this, or something simple I'm missing. Any help is much appreciated! Thanks!


Is session-state enabled on your site? It can be disabled in a couple of different way, on a page level or even in web.config.

You should also be aware the Page_Load event fires for every request, before the check-box auto-postback happens. I'm also a little confused as to what you're trying to store: I assume every row has a check-box, but it seems you're trying to store the set/not-set value in a single session variable. How do you differentiate which have been selected, and which ones hasn't? :)

Update: Okay, let's try a clean the code up a little bit. First create a property to access the session value:

Private Property ShowHiddenRotations As Boolean
  Get
    If Not Session("ShowHiddenRotations") Is Nothing Then
      Return CType(Session("ShowHiddenRotations"), Boolean)
    Else
      Return False
    End If
  End Get
  Set(value As Boolean)
    Session("ShowHiddenRotations") = value
  End Set
End Property

If you're using that value on other pages, I would recommend moving it to a seperate class.

Then we can reduce your other code to something closer to this:

Protected Sub ShowHiddenCheckBoxChange(ByVal sender As Object, ByVal e As EventArgs)
  ShowHiddenRotations = ShowHiddenCheckbox.Checked
End Sub

And ...

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load   
  If not Page.IsPostBack
      ' Load your data, better stick it in a seperate sub...
      ShowHiddenCheckBox.Checked = ShowHiddenRotations
  else
      ' This section is executed BEFORE any control methods are run, i.e. ShowHiddenCheckBoxChange
  end if
End Sub

I'm guessing your problem is really just the order of how things are called in your page. What happens when you debug through it?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜