?
Solved

Persistent ArrayLists between pageviews in ASP.NET

Posted on 2003-03-07
4
Medium Priority
?
317 Views
Last Modified: 2008-03-06
Hey all,
I've got an ArrayList in my ASP.NET page. Basically, I want the user to be able to add items into an arraylist by pressing a button. However, each time they do this the page reloads and their array is initialized to nothing.

1) Where should I put my line: array = new ArrayList(0); in order to avoid re-initialization
2) How do I make the arraylist persist after the button is clicked?

Thanks,
-Dan
0
Comment
Question by:SuperMario
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:Hummusx
ID: 8092550
Well, what you want to do is wrap your initialization in an if statement like this:


If Not IsPostBack() Then
  'Initialize
Else
  'Restore from wherever you stash it
End If

You can either put it in Viewstate or in Session.  It depends how large it will be and how much of a load you will have on your server.  If you aren't worried about server load, just stick it in the session state when you are done with it on the page:

Session("myArrayList") = myArrayList

Then in the "else" portion do something like this:

myArrayList = CType(Session("myArrayList"), ArrayList)


So basically if the user is just coming to the page, it will run the initialization and create the object.  Otherwise, it will reinstate the one saved in the session.  If you want to keep it between pages, you can change your if statement to:

If Session("myArrayList") Is Nothing Then
  'Initialize
Else
  myArrayList = CType(Session("myArrayList"), ArrayList)
End If

So in this case it will always check to see if it exists in the session.  If not, it will create it.
0
 
LVL 2

Accepted Solution

by:
Hummusx earned 500 total points
ID: 8092552
Erg sorry I posted it in VB.  I keep doing that.  It should be close enough for you to move into C#.  If not, let me know and I can translate it.
0
 
LVL 3

Author Comment

by:SuperMario
ID: 8092559
I'll give this a try, it sounds like it will work. And don't worry about the VB. I wrote a lot of classic ASP 3.0 over the past couple of years ;) I miss the ADODB.Recordset object...
0
 
LVL 3

Author Comment

by:SuperMario
ID: 8092681
That did the trick! Thank you Hummusx. Your help's much appreciated.

-Dan
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question