ViewState too large

G

Guest

Is it possible to store the same information about a control that would be
saved in the ViewState in a Session state? I have a page with three treeview
controls and if I enable the view state for the controls I get huge delays on
postbacks, but if I disable the viewstate I lose all my information. Any
sugesstions?
 
K

Kumar Reddi

Well seems like your solution is to look for otherways to store viewstate,
rather than as part of the page html. do a search for storing viewstate in
session or db in google. For a start take a look at the following article

http://aspalliance.com/472

Good Luck
 
W

William F. Robertson, Jr.

There are probably several ways to tackle this but here is how I have done
it in the past.

You will need to override two methods for your control.
protected override void LoadViewState(object savedState);
protected override object SaveViewState()

SaveViewState returns the object that should be placed into viewstate. So
you intercept it and store it into session

protected override object SaveViewState()
{
//this will load the viewstate as normal
object o = base.SaveViewState();

//store it into session
System.Web.HttpContext.Current.Session[this.ID] = o;

//i think you need to return something (not null) in order for
LoadViewState
return new object();
}

protected override void LoadViewState( object savedState )
{
//load the savedState from Session
savedState = System.Web.HttpContext.Current.Session[this.ID];

//call base constructor
base.LoadViewState( savedState );
}

HTH,

bill
 
O

Ollie

surely storing the view state on the server could lead to different
behaviour when the user makes use of the 'back' and forward' buttons on a
browser?

Ollie Riches

William F. Robertson said:
There are probably several ways to tackle this but here is how I have done
it in the past.

You will need to override two methods for your control.
protected override void LoadViewState(object savedState);
protected override object SaveViewState()

SaveViewState returns the object that should be placed into viewstate. So
you intercept it and store it into session

protected override object SaveViewState()
{
//this will load the viewstate as normal
object o = base.SaveViewState();

//store it into session
System.Web.HttpContext.Current.Session[this.ID] = o;

//i think you need to return something (not null) in order for
LoadViewState
return new object();
}

protected override void LoadViewState( object savedState )
{
//load the savedState from Session
savedState = System.Web.HttpContext.Current.Session[this.ID];

//call base constructor
base.LoadViewState( savedState );
}

HTH,

bill


clsmith66 said:
Is it possible to store the same information about a control that would
be
saved in the ViewState in a Session state? I have a page with three treeview
controls and if I enable the view state for the controls I get huge
delays on
postbacks, but if I disable the viewstate I lose all my information. Any
sugesstions?
 
B

bruce barker

you store a guid on the page in a hidden field, so you can find the matching
viewstate. if the viewstate can not be found (deleted from cache) redirect
to the page.

-- bruce (sqlwork.com)

"Ollie" <why do they need this!!!!> wrote in message
| surely storing the view state on the server could lead to different
| behaviour when the user makes use of the 'back' and forward' buttons on a
| browser?
|
| Ollie Riches
|
| "William F. Robertson, Jr." <theman_at_fdrsucks.com> wrote in message
| | > There are probably several ways to tackle this but here is how I have
done
| > it in the past.
| >
| > You will need to override two methods for your control.
| > protected override void LoadViewState(object savedState);
| > protected override object SaveViewState()
| >
| > SaveViewState returns the object that should be placed into viewstate.
So
| > you intercept it and store it into session
| >
| > protected override object SaveViewState()
| > {
| > //this will load the viewstate as normal
| > object o = base.SaveViewState();
| >
| > //store it into session
| > System.Web.HttpContext.Current.Session[this.ID] = o;
| >
| > //i think you need to return something (not null) in order for
| > LoadViewState
| > return new object();
| > }
| >
| > protected override void LoadViewState( object savedState )
| > {
| > //load the savedState from Session
| > savedState = System.Web.HttpContext.Current.Session[this.ID];
| >
| > //call base constructor
| > base.LoadViewState( savedState );
| > }
| >
| > HTH,
| >
| > bill
| >
| >
| > | >> Is it possible to store the same information about a control that would
| >> be
| >> saved in the ViewState in a Session state? I have a page with three
| > treeview
| >> controls and if I enable the view state for the controls I get huge
| >> delays
| > on
| >> postbacks, but if I disable the viewstate I lose all my information.
Any
| >> sugesstions?
| >
| >
|
|
 
O

Ollie

nice idea I like it are there any drawbacks to this approach of handling
viewstate?

Ollie Riches
 
W

William F. Robertson, Jr.

The size will become prohibitive if you site has a lot of hits, and you have
to decide at what point you want to remove the server side storage of the
viewstate. IOW how many "backs" do you want to allow your user to do?

And then you have to decide how you want to handle no viewstate recorded for
the page. (you have purged the record from memory).

The easiest (and arguably lamest way), is to save the viewstate with the
hidden field GUID suggested by Bruce. Then if the GUID doesn't match the
GUID of the object saved into Session, you could display and error message
for the user telling them, they can't hit the back button, please use the
navigation provided on the page.

I am not sure what type of information you are saving in viewstate, but on a
page I was serializing a DataTable to the page, it become prohibitive in
viewstate after 20 records or so (noticeably slower). I just saved the
query used to build the DataTable into viewstate, much smaller. I am
placing more load on my SQL, but Sql is better at running queries than the
framework deserializing 30K of viewstate.

So you might be able to save the instructions for creating the large amount
of data and you could bypass the whole issue entirely.

bill


Ollie said:
nice idea I like it are there any drawbacks to this approach of handling
viewstate?

Ollie Riches

bruce barker said:
you store a guid on the page in a hidden field, so you can find the matching
viewstate. if the viewstate can not be found (deleted from cache) redirect
to the page.

-- bruce (sqlwork.com)

"Ollie" <why do they need this!!!!> wrote in message
| surely storing the view state on the server could lead to different
| behaviour when the user makes use of the 'back' and forward' buttons
on
a
| browser?
|
| Ollie Riches
|
| "William F. Robertson, Jr." <theman_at_fdrsucks.com> wrote in message
| | > There are probably several ways to tackle this but here is how I have
done
| > it in the past.
| >
| > You will need to override two methods for your control.
| > protected override void LoadViewState(object savedState);
| > protected override object SaveViewState()
| >
| > SaveViewState returns the object that should be placed into viewstate.
So
| > you intercept it and store it into session
| >
| > protected override object SaveViewState()
| > {
| > //this will load the viewstate as normal
| > object o = base.SaveViewState();
| >
| > //store it into session
| > System.Web.HttpContext.Current.Session[this.ID] = o;
| >
| > //i think you need to return something (not null) in order for
| > LoadViewState
| > return new object();
| > }
| >
| > protected override void LoadViewState( object savedState )
| > {
| > //load the savedState from Session
| > savedState = System.Web.HttpContext.Current.Session[this.ID];
| >
| > //call base constructor
| > base.LoadViewState( savedState );
| > }
| >
| > HTH,
| >
| > bill
| >
| >
| > | >> Is it possible to store the same information about a control that would
| >> be
| >> saved in the ViewState in a Session state? I have a page with three
| > treeview
| >> controls and if I enable the view state for the controls I get huge
| >> delays
| > on
| >> postbacks, but if I disable the viewstate I lose all my information.
Any
| >> sugesstions?
| >
| >
|
|
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top