Problem saving arraylist to viewstate

  • Thread starter Thread starter Jim Corey
  • Start date Start date
J

Jim Corey

I'm trying to save some ArrayLists to viewstate
and then use them as DataSources for dropdownboxes.

The code looks like this:

'DptList is a arraylist variable local to this procedure

'...populate DptList
viewstate("Dept01") = DptList
DptList.Clear()
'...populate DptList
viewstate("Dept02") = DptList
DptList.Clear() 'Comment this line and it works

When I try to use viewstate("Dept01") in another procedure
as the datasource it doesn't work. I cast this back to
an arraylist and the count is 0.

If I comment out the second DptList.Clear() it works fine.
I'm stumped.
This is all on the initial load of the page.

TIA
Jim
 
Car1.Driver = Joe
Joe.Kill()
Resurrect(Joe)
Car2.Driver = Joe
Joe.Kill()

Is the driver of Car1 still alive? (No, you killed him after resurrecting
him)

Car1.Driver = Joe
Joe.Kill()
Resurrect(Joe)
Car2.Driver = Joe

Is the driver of car1 still alive? (Yes, you resurrected him after killing
him)

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Well, yes.
I guess I discounted this because (1) the first arraylist
displayed (in the dropdown) as it existed before it got
cleared the first time, and (2) I somehow hoped that
creating a viewstate variable wouldn't maintain the reference to the
arraylist. (If that's how you phrase it).

Thanks, I'll go play with it some more.

Jim
 
viewstate serializes your objects at render time (it will not deserialize
until the postback). this means its just a reference to the passed object.
therefore viewstate("Dept01") and viewstate("Dept02") both reference the
same object (DptList).

note: on postback when deserialized, viewstate("Dept01") and
viewstate("Dept02") will reference two seperate arraylists.

-- bruce (sqlwork.com)
 
Back
Top