Saving/restoring viewstate between different pages..

R

RCS

I have an app that I have different "sections" that I want to switch back
and forth from, all while having the server maintain viewstate for each
page. In other words, when I am on Page1.aspx and set textboxes, radio
buttons, etc - that viewstate is fine. Then I have a linkbutton that does a
Server.Transfer over to Page2.aspx. When I Server.Transfer back to
Page1.aspx, the viewstate info is lost.

I ran across another example of this last weekend too - imagine filling out
a registration and order form, type pages.. and once you click "Review"
(which sends you to a different page) - if you want to go back to the last
page to change your information, the viewstate is lost.

ANYhow, so what I've done is on the linkbutton that lets you leave the page,
I put:

Session.Add("QuestionnaireViewstate",this.ViewState);

Server.Transfer("Default.aspx",true);



Then, when you come back to this page, I put this in page_load:

StateBag objSB = ((StateBag)Session["QuestionnaireViewstate"]);

if ( objSB != null )

{

Response.Write(objSB.Count + " is number of items<br>");

foreach (string val in objSB.Keys)

{

Response.Write("Setting '" + val + "' to '" + objSB[val].ToString() +
"'<br>");

this.ViewState[val] = objSB[val].ToString();

}

}

else

{

Response.Write("StateBag is null");

}



Turns out that this.ViewState has 0 items before it leaves - otherwise, this
seems like it might work. Any ideas on how to preserve viewstate between
pages and allowing the user to click back and forth between pages - all
while keeping thier viewstate data alive?? Thanks
 
R

Random

I think your understanding of ViewState is confused.

What is the purpose of you preserving content between pages? You're using
the Session, which is really not intended for temporary storage of page
data. And even if you do want it in the Session, why would you preserve it
in the ViewState on top of that?

What you may want to do instead is have all your functionality on one page,
inside sections you can set/reset the Visibility on. That way, you don't
have to play around with manually handling the Session and/or ViewState at
all. It will all be handled automatically for you.
 
R

RCS

Normally, I'd completely agree with you. With that said, that IS what *EACH*
page does. EACH page has several visible/invisible panels (that make up
virtual pages, or tabs) that have all sorts of information - that results in
a several dozen (if not hundreds of) controls on each page. I want to break
up the functionality further into logical chunks, because to do this all on
one page is just WAY too much.

I ended up spending the whole day on this and ran into brick wall after
brick wall. I am in the process of just writing my own little handler for
basically copy the pertinent properties (like .Text, .SelectedIndex, etc)
into a hash table (stored by control ID and property), then store that hash
table in a session variable.

It REALLY seems to me, and REALLY frustrates me that I have to do this. It
just really seems that since "something" keeps the viewstate information for
a page, I should be able to cache that in a session variable, then re-apply
it later if I come back to that page. Instead, I have to hack something
together??

Anyone else have any other ideas on a more elegant way to do this?
Difficulty: I've messed around with serializing the form, saving the form
off to a session variable and overrriding Save/Load viewstate from medium
methods. The closest I got, was copying the instantiated form off to a
session variable, then re-applying it when you come back to the page. The
*first* load works fine, but if you re-submit the page, it loses all it's
viewstate info (I assume because OnInit built it's viewstate info off of
what IT did)..


Grrr....... :-(


Random said:
I think your understanding of ViewState is confused.

What is the purpose of you preserving content between pages? You're using
the Session, which is really not intended for temporary storage of page
data. And even if you do want it in the Session, why would you preserve
it in the ViewState on top of that?

What you may want to do instead is have all your functionality on one
page, inside sections you can set/reset the Visibility on. That way, you
don't have to play around with manually handling the Session and/or
ViewState at all. It will all be handled automatically for you.



RCS said:
I have an app that I have different "sections" that I want to switch back
and forth from, all while having the server maintain viewstate for each
page. In other words, when I am on Page1.aspx and set textboxes, radio
buttons, etc - that viewstate is fine. Then I have a linkbutton that does
a Server.Transfer over to Page2.aspx. When I Server.Transfer back to
Page1.aspx, the viewstate info is lost.

I ran across another example of this last weekend too - imagine filling
out a registration and order form, type pages.. and once you click
"Review" (which sends you to a different page) - if you want to go back
to the last page to change your information, the viewstate is lost.

ANYhow, so what I've done is on the linkbutton that lets you leave the
page, I put:

Session.Add("QuestionnaireViewstate",this.ViewState);

Server.Transfer("Default.aspx",true);



Then, when you come back to this page, I put this in page_load:

StateBag objSB = ((StateBag)Session["QuestionnaireViewstate"]);

if ( objSB != null )

{

Response.Write(objSB.Count + " is number of items<br>");

foreach (string val in objSB.Keys)

{

Response.Write("Setting '" + val + "' to '" + objSB[val].ToString() +
"'<br>");

this.ViewState[val] = objSB[val].ToString();

}

}

else

{

Response.Write("StateBag is null");

}



Turns out that this.ViewState has 0 items before it leaves - otherwise,
this seems like it might work. Any ideas on how to preserve viewstate
between pages and allowing the user to click back and forth between
pages - all while keeping thier viewstate data alive?? Thanks
 
R

RCS

Just as a followup for anyone else who is looking for an answer to this, it
seems what I was attempting to do, wasn't possible.

However, it dawned on me there is another - and dare I say better way to do
this. I took all of my major chunks of functionality, and put them into User
Controls on ONE page. Then, I have my section navigation just set the
..Visible = true; or false to whichever controls (which contain chunks of
functionality) I need. Best of all, It keeps viewstate for all the controls
(visible or not) within all of the User Controls. So I can easily switch
between which user control is visible!

Anyhow, this was a pretty specific situation that I needed and this worked
out quite well. For whatever it's worth....


RCS said:
Normally, I'd completely agree with you. With that said, that IS what
*EACH* page does. EACH page has several visible/invisible panels (that
make up virtual pages, or tabs) that have all sorts of information - that
results in a several dozen (if not hundreds of) controls on each page. I
want to break up the functionality further into logical chunks, because to
do this all on one page is just WAY too much.

I ended up spending the whole day on this and ran into brick wall after
brick wall. I am in the process of just writing my own little handler for
basically copy the pertinent properties (like .Text, .SelectedIndex, etc)
into a hash table (stored by control ID and property), then store that
hash table in a session variable.

It REALLY seems to me, and REALLY frustrates me that I have to do this. It
just really seems that since "something" keeps the viewstate information
for a page, I should be able to cache that in a session variable, then
re-apply it later if I come back to that page. Instead, I have to hack
something together??

Anyone else have any other ideas on a more elegant way to do this?
Difficulty: I've messed around with serializing the form, saving the form
off to a session variable and overrriding Save/Load viewstate from medium
methods. The closest I got, was copying the instantiated form off to a
session variable, then re-applying it when you come back to the page. The
*first* load works fine, but if you re-submit the page, it loses all it's
viewstate info (I assume because OnInit built it's viewstate info off of
what IT did)..


Grrr....... :-(


Random said:
I think your understanding of ViewState is confused.

What is the purpose of you preserving content between pages? You're
using the Session, which is really not intended for temporary storage of
page data. And even if you do want it in the Session, why would you
preserve it in the ViewState on top of that?

What you may want to do instead is have all your functionality on one
page, inside sections you can set/reset the Visibility on. That way, you
don't have to play around with manually handling the Session and/or
ViewState at all. It will all be handled automatically for you.



RCS said:
I have an app that I have different "sections" that I want to switch back
and forth from, all while having the server maintain viewstate for each
page. In other words, when I am on Page1.aspx and set textboxes, radio
buttons, etc - that viewstate is fine. Then I have a linkbutton that does
a Server.Transfer over to Page2.aspx. When I Server.Transfer back to
Page1.aspx, the viewstate info is lost.

I ran across another example of this last weekend too - imagine filling
out a registration and order form, type pages.. and once you click
"Review" (which sends you to a different page) - if you want to go back
to the last page to change your information, the viewstate is lost.

ANYhow, so what I've done is on the linkbutton that lets you leave the
page, I put:

Session.Add("QuestionnaireViewstate",this.ViewState);

Server.Transfer("Default.aspx",true);



Then, when you come back to this page, I put this in page_load:

StateBag objSB = ((StateBag)Session["QuestionnaireViewstate"]);

if ( objSB != null )

{

Response.Write(objSB.Count + " is number of items<br>");

foreach (string val in objSB.Keys)

{

Response.Write("Setting '" + val + "' to '" + objSB[val].ToString() +
"'<br>");

this.ViewState[val] = objSB[val].ToString();

}

}

else

{

Response.Write("StateBag is null");

}



Turns out that this.ViewState has 0 items before it leaves - otherwise,
this seems like it might work. Any ideas on how to preserve viewstate
between pages and allowing the user to click back and forth between
pages - all while keeping thier viewstate data alive?? Thanks
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top