Retrieving data after postback

G

Guest

If have an asp.net page (code behind written in VB.NET) which needs to store
information from a postback in an arraylist and retain this list across
multiple postbacks.

The variable is declared at the class level as:

Public files As New ArrayList

And I am accessing it in two event procedures. (fileList is an ap:listbox
control and fileUploadBox is an HtmlInputFile control with runat="server"):

Private Sub addFile_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles addFile.Click
If IsPostBack And fileUploadBox.Value <> "" Then
files.Add(fileUploadBox)
fileList.Items.Add(fileUploadBox.PostedFile.FileName)
End If
End Sub

Private Sub removeFile_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles removeFile.Click
If fileList.Items.Count <> 0 And fileList.SelectedIndex <> -1 Then
files.RemoveAt(fileList.SelectedIndex)
fileList.Items.Remove(fileList.SelectedItem.Text)
End If
End Sub

The addFile_Click procedure works fine and the files arraylist is updated;
however, when the removeFile_Click procedure runs the files arraylist is
always empty.

I thought that the contents of any global variables was automatically
maintained across postbacks but this doesn't seems to be working. How can I
do this?

Many thanks for any help.

Alan
 
E

Eliyahu Goldin

Alan,

Being public has nothing to do with life time. You need to save it in either
a session variable or in the application cache. Or make it static. Or
repopulate it on every postback. Or put it into viewstate. Many choices.

Eliyahu
 
G

Guest

Thanks for the advice. Got it sorted now.

Alan

Eliyahu Goldin said:
Alan,

Being public has nothing to do with life time. You need to save it in either
a session variable or in the application cache. Or make it static. Or
repopulate it on every postback. Or put it into viewstate. Many choices.

Eliyahu
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top