views:

29

answers:

4

I have a webapp in ASP.Net with a VB codebehind. I need a List variable I have declared to persist as long as the person is on the page, but currently any time a control posts back to the code, everything is cleared out. I am completely new to ASP.net, so I have no idea if this is even possible. Can it be done with a Session variable? Those seem to me to be limited to base types, but I could be wrong.

+2  A: 

With ASP.Net it is creating a new instance of your Code Behind class each time the page loads, which is why your class-level variables are being reset.

Your best option is probably to store the List you are wanting to hold on to in the Session. If it would make things easier you can create a getter for the list that reads the data from the session variable. In C# it would look like:

private List MyList
{
    get { return Session["ListKey"] as List; }
    set { Session["ListKey"] = value; }
}
ckramer
He's using VB :)
Francisco Soto
Would I just declare a list and save it to the session every time, like: `Session("ListOfStuff") = codeBehindList` ?
Caleb Thompson
@Francisco: Thanks, but I can probably manage to translate from C# to VB :)
Caleb Thompson
Just for clarification, Session is specific to a user, not to the page itself, correct? So if two people are using the site at once the Session variable won't be passing data between them?
Caleb Thompson
That's right, Session and ViewState are specific to the user.
Francisco Soto
@Chapso You'll only want to create a new list if the call to Session["ListKey"] returns NULL. If you re-assign it each time you will be overwriting the previously stored vales with an empty list. One alternative may be to make the property read-only, and do a check in the getter to see if the value is null in the session.
ckramer
@Chapso I can translate to VB.Net if you need it....it just takes me longer :)
ckramer
No worries. I've got it working. I set the List to a new one on page load, and that worked well.
Caleb Thompson
+3  A: 

Look into the ASP.NET ViewState. You should also know that lots of gotchas if you do use it including page size bloat and performance issues. The C# code (sry. I'm not good at VB) would be:

List<int> MyList
{
    get { return (List<int>) ViewState["mylist"]; }
    set { ViewState["mylist"] = value; }
}

Note: Don't forget to initialize this variable.

Note on using Session State: This won't work if a user has 2 or more windows open at the same time.

Keltex
heh, forgot about viewstate...goes to show how little I like it :). Probably the better option tho.
ckramer
I guess I sound pretty new, but I am at this, so that's fine. Why would viewstate be a better option when it slows down the page?
Caleb Thompson
@Chapso - It's not a horrible option. For example if you're storing a List of a small number on ints, you are probable OK. If you are storing thousands of ints, you might want to look elsewhere.
Keltex
+1  A: 

If you want to save a variable that is only needed in that particular page, not in any other page, assuming you have a multipage web application, you can use your page ViewState to save variables that you want to persist from request to request.

If you need your variable to persist among different pages, you can use the Session to save it.

Now, when you use your ViewState you must take in account that if you store big objects there, or have many many controls in your page, a huge string (encoded viewstate) goes back and forth between postbacks, increasing the load times and execution times.

Francisco Soto
A: 

Here's the code I ended up using.

Private Property ChangedControls() As List(Of Control)
    Get
        Return DirectCast(Session("changedControls"), List(Of Control))
    End Get
    Set(ByVal value As List(Of Control))
        Session("changedControls") = value
    End Set
End Property

Assigning a new list on page load worked just fine.

Caleb Thompson