Problem with session variables in Page_Load

I

itissandeep

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
 
I

itissandeep

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
 
D

Darren Kopp

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/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top