Asp.net stores cookie / session for checkbox

I have a checkbox and I want to achieve this, if I checked the checkbox, after reloading the page, it would still check (I have no value stored in the database).

Checkbox

  <asp:CheckBox ID="cb1" runat="server"  />

      

Checkbox event will change it

  Protected Sub cb1_CheckedChanged(sender As Object, e As EventArgs) Handles cb1.CheckedChanged
    Dim SE_cb1 As Boolean = Nothing
    SE_cb1 = If(cb1.Checked = True, 1, 0)
    Session("SE_cb1") = SE_cb1

End Sub

      

Page load event

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      If Not Session("SE_cb1") Is Nothing Then cb1.Checked = If(Session("SE_cb1") = 1, True, False)

    end sub

      

I wonder why, after I reloaded / redirected the page, the checked checkbox still dropped the check?

Thanks for any advice!

+3


source to share


1 answer


OK, when I change the load event to prerender it now seems OK. Thanks for the help!



Handles Me.Load → Handles Me.PreRender

0


source







All Articles