Add to Array in Session with checkbox handler

G

gce

What happens :

When I first press the button, I get an listbox1 with a,b,c (correct:
because of the
addtoa(1, "a")
addtoa(2, "b")
addtoa(3, "c")

When I check the checkbox, the eventhandler triggers and when I press the
button, the addtoa(4,"d") works fine, because I have a listbox with a,b,c,d

But now the strange thing. When I press the button again, the d is lost
again. This is just a testcode, but in my program I have the same problem
with adding something to an array inside a handler using the session var.

Please help me !


Public Class frmArrayTest1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents ListBox1 As System.Web.UI.WebControls.ListBox
Protected WithEvents CheckBox1 As System.Web.UI.WebControls.CheckBox
Protected WithEvents Button1 As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

Dim a
ReDim Preserve a(4, 4)

AddHandler CheckBox1.CheckedChanged, AddressOf
CheckBox1_CheckedChanged

Session("test") = a
addtoa(1, "a")
addtoa(2, "b")
addtoa(3, "c")

End Sub

Private Sub addtoa(ByVal intIndex, ByVal strString)

Dim a

'ListBox1.Items.Add(strString)

a = Session("test")
a(intIndex, 2) = strString
Session("test") = a


End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles CheckBox1.CheckedChanged
addtoa(4, "d")

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim x
Dim i

x = Session("test")
ListBox1.Items.Clear()
For i = 1 To UBound(x)
ListBox1.Items.Add(Str(i) & "-" & x(i, 2))
Next i
End Sub
End Class
 
L

Lars-Erik Aabech

Hi!

I'm no VB guru (love C# ;) ), but I have a feeling you might get a little
bit more predictable code if you don't re-initialize the array in Page_Load
each time. There's a property on Page called IsPostBack which is true if the
request is a postback (i.e. the user clicked a control)

Try modifying your Page_Load code like this so that the array isn't
initialized when the user clicks the button:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here


Dim a
ReDim Preserve a(4, 4)

Session("test") = a
addtoa(1, "a")
addtoa(2, "b")
addtoa(3, "c")

End Sub
 
L

Lars-Erik Aabech

Sorry, keyboard catched a non-intended ctrl+enter ;)
Here's the code I was proposing:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

AddHandler CheckBox1.CheckedChanged, AddressOf
CheckBox1_CheckedChanged

' Without If Not IsPostBack, array is rebuilt on each roundtrip, and
your previous data is lost
If Not IsPostBack Then
Dim a
ReDim Preserve a(4, 4)

addtoa(1, "a")
addtoa(2, "b")
addtoa(3, "c")
Session("test") = a
End If

End Sub
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top