Problem with session variables in Page_Load

Discussion in 'ASP .Net' started by itissandeep@gmail.com, Mar 7, 2006.

  1. Guest

    I have a simple application that has a map. The user has to select two
    sets from a single type of feature from the map. I use two radio
    buttons under same GroupName for each set.

    When the page loads for the first time, one radio button is checked
    (obviously the other is unchecked). The user selects for one set and
    checks the other radio button. Since, I have the AutoPostBack to be
    true, the page loads again and the radio button for the second set is
    checked. The state of the radio buttons is stored in session variables.

    Everything is fine till now. Now starts the problem and struggle (!!).
    With the second radio button checked, when I select any feature to add
    to the second set, the selected features get added to the first set and
    the radio button for the first set is checked and that for the second
    set is unchecked on its own. I observed that this happens in the
    Page_Load method only (Please refer to the code below). In other
    methods the session variables are as intended

    I have been struggling with this problem from many days. Can someone
    please help ? Thank you very much

    The code is the following:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load


    Dim sMode As String
    Dim iMode As Integer = 0

    If Not Page.IsPostBack Then
    RB1.Checked = Session("Checked1")
    RB2.Checked = Session("Checked2")
    Me.sUrl = CreateStreetMap(1)
    End If



    End Sub

    Public Function CreateStreetMap(ByVal AMode As Integer) As String
    ....
    ....
    ....
    Dim s1 As String
    Dim s2 As String

    If (RB1.Checked = True) Then
    s1 = Request.QueryString("fieldname")
    If Not s1 = "" Then
    Session("Collection1") = _
    Session("Collection1") & "," & s1
    End If
    End If

    If (RB2.Checked = True) Then
    s2 = Request.QueryString("fieldname")
    If Not s2 = "" Then
    Session("Collection2") = _
    Session("Collection2") & "," & s2
    End If
    End If
    TextBox1.Text = Session("Collection1")
    TextBox2.Text = Session("Collection2")
    ....
    ....
    ....
    End Function

    Private Sub RB1_CheckedChanged(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles IncidentLinkRB.CheckedChanged
    Session("Checked1") = RB1.Checked
    Session("Checked2") = Not (RB2.Checked)
    Session("checkChanged") = True

    Me.sUrl = CreateStreetMap(1)

    End Sub

    Private Sub RB2_CheckedChanged(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles DetourLinkRB.CheckedChanged
    Session("Checked1") = Not (RB2.Checked)
    Session("Checked2") = RB2.Checked
    Session("checkChanged") = True

    Me.sUrl = CreateStreetMap(1)

    End Sub
    , Mar 7, 2006
    #1
    1. Advertising

  2. Darren Kopp Guest

    In RB2_CheckedChanged you have Session("Checked1") being set to
    Not(RB2.Checked). Shouldn't that be Not(RB1.Checked)?

    HTH,
    Darren Kopp
    http://blog.secudocs.com/
    Darren Kopp, Mar 7, 2006
    #2
    1. Advertising

  3. Guest

    Hi Darren,
    I tried both:
    Session("Checked1") = Not (RB1.Checked)
    and Session("Checked1") = RB1.Checked
    and likewise in RB1_CheckChanged. The result is the same.

    Sandeep
    , Mar 7, 2006
    #3
  4. Darren Kopp Guest

    I would recommend using the RadioButtonList class if you can only
    select one radiobutton at a time, then just figure out which one is
    selected using the SelectedItem property, then bind based on that
    logic. If you can simultaneously select both, then use a CheckBox
    control. If that's not possible, then I would make a few corrections
    to your code as such.

    in the page load section, i would use this code

    ' --------------------------------------
    If Not Page.IsPostBack Then
    RB1.Checked = CType(Session("Checked1"), Boolean)
    RB2.Checked = CType(Session("Checked2"), Boolean)
    Me.sUrl = CreateStreetMap(1)
    End If
    ' --------------------------------------

    I would also modify the following OnCheckedEvents

    ' --------------------------------------
    Private Sub RB1_CheckedChanged(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles IncidentLinkRB.CheckedChanged
    Session("Checked1") = RB1.Checked
    ' Session("Checked2") = Not (RB2.Checked) - commented out, session
    should already have this value? if not, initialize Session with
    default values
    Session("checkChanged") = True


    Me.sUrl = CreateStreetMap(1)


    End Sub


    Private Sub RB2_CheckedChanged(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles DetourLinkRB.CheckedChanged
    ' Session("Checked1") = Not (RB2.Checked) - commented out - session
    should already have this value, if not, initialize session with default
    values earlier
    Session("Checked2") = RB2.Checked
    Session("checkChanged") = True


    Me.sUrl = CreateStreetMap(1)


    End Sub
    ' ---------------------------------------------------------

    When i say initialize earlier, i mean something in the If Not
    Page.IsPostBack adding something like this

    '------------------------------------------------------------
    If Session("Checked1") = Nothing Then
    Session("Checked1") = false
    End If

    If Session("Checked2") = Nothing Then
    Session("Checked2") = false
    End If

    '------------------------------------------------

    That way, you won't accidently overwrite your values if that is what
    may be happening.

    Let me know if you are still having problems. If needed, you can email
    me the full code at darrenkopp [at] [gmail dot com] and I will look
    through it more in depth.

    -Darren Kopp
    http://blog.secudocs.com/
    Darren Kopp, Mar 7, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bminder
    Replies:
    1
    Views:
    2,151
    nfedin
    Feb 23, 2004
  2. z. f.
    Replies:
    0
    Views:
    549
    z. f.
    Oct 19, 2004
  3. Carlos
    Replies:
    2
    Views:
    391
    Mark Fitzpatrick
    Feb 25, 2005
  4. rockdale
    Replies:
    1
    Views:
    647
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Nov 16, 2006
  5. Replies:
    9
    Views:
    925
Loading...

Share This Page