R
Ryan Ternier
I'm playing around with Cache and Viewstate.
I've made a small little example that reminds me of grade 12:
if(!IsPostBack)
{
ArrayList alTest = new ArrayList();
alTest.Add("This is from the ViewState");
alTest.Add("This is another from the viewstate");
alTest.Add("Wow, ViewState rocks for this");
ViewState["alTest"] = alTest;
alTest.Clear();
alTest.Add("This is from the CACHE!");
alTest.Add("EL CACHEO! CACHORAMA!");
alTest.Add("hi");
Cache["alTest"] = alTest;
}
else
{
foreach(string s in (ArrayList)ViewState["alTest"])
Response.Write("<BR>" + s);
Response.Write("<BR><BR>");
foreach(string sr in (ArrayList)Cache["alTest"])
Response.Write("<BR>" + sr);
}
When it posts back I get the results from the Cache, even when it reads
the Arraylist.
Now, i know this isn't because I used the name "alTest" for both the
ViewState and the Chache.
So I debugged through, and stopped when i hit the last line:
Cache["alTest"] = alTest;
When I looked... The ViewState and the Cache had the exact same values.
So I made a new Array for the Cache as follows:
alTestCache.Add("This is from the CACHE!");
alTestCache.Add("EL CACHEO! CACHORAMA!");
alTestCache.Add("hi");
Cache["alTest"] = alTestCache;
When I did this, I got the results I thought I would get....
I played more.... and it seems when: alTest.Clear is called
It modifies the viewstate that is bound to that object.
So when I clear it, it clears the ViewState.
Is there a way of stopping this? So you can save an object to the
ViewState, and then go and modify the contents of it, without it
affecting the view state (probably inneficient to do so, but I just want
know if there's a way).
/RT
I've made a small little example that reminds me of grade 12:
if(!IsPostBack)
{
ArrayList alTest = new ArrayList();
alTest.Add("This is from the ViewState");
alTest.Add("This is another from the viewstate");
alTest.Add("Wow, ViewState rocks for this");
ViewState["alTest"] = alTest;
alTest.Clear();
alTest.Add("This is from the CACHE!");
alTest.Add("EL CACHEO! CACHORAMA!");
alTest.Add("hi");
Cache["alTest"] = alTest;
}
else
{
foreach(string s in (ArrayList)ViewState["alTest"])
Response.Write("<BR>" + s);
Response.Write("<BR><BR>");
foreach(string sr in (ArrayList)Cache["alTest"])
Response.Write("<BR>" + sr);
}
When it posts back I get the results from the Cache, even when it reads
the Arraylist.
Now, i know this isn't because I used the name "alTest" for both the
ViewState and the Chache.
So I debugged through, and stopped when i hit the last line:
Cache["alTest"] = alTest;
When I looked... The ViewState and the Cache had the exact same values.
So I made a new Array for the Cache as follows:
alTestCache.Add("This is from the CACHE!");
alTestCache.Add("EL CACHEO! CACHORAMA!");
alTestCache.Add("hi");
Cache["alTest"] = alTestCache;
When I did this, I got the results I thought I would get....
I played more.... and it seems when: alTest.Clear is called
It modifies the viewstate that is bound to that object.
So when I clear it, it clears the ViewState.
Is there a way of stopping this? So you can save an object to the
ViewState, and then go and modify the contents of it, without it
affecting the view state (probably inneficient to do so, but I just want
know if there's a way).
/RT