ViewState question - for tech-heads only

G

Gandalf

Say I have persisted a DataSet in the ViewState of a page, and that when the
page is posted back I make several references to that dataset like so:

DataSet MyDataSet = (DataSet)ViewState["VsDataSet"]; //C#

Does this have to deserialize the dataset every time it is referenced... or
is it only done once when the page is posted back? I'm just wondering if I
should assign the DataSet to a local variable in the PageLoad method or if
it's OK to continually reference it via the ViewState.

TIA
 
T

Teemu Keiski

Hi,

ViewState collection is populated from serialized string at LoadViewState
phase once on the postback, so it does not basically deserialize it every
time you reference it. But certainly its not bad habit if you make a
reference to it in local variable (less need for casting).

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist

Say I have persisted a DataSet in the ViewState of a page, and that when the
page is posted back I make several references to that dataset like so:

DataSet MyDataSet = (DataSet)ViewState["VsDataSet"]; //C#

Does this have to deserialize the dataset every time it is referenced... or
is it only done once when the page is posted back? I'm just wondering if I
should assign the DataSet to a local variable in the PageLoad method or if
it's OK to continually reference it via the ViewState.

TIA
 
G

Gandalf

Thanks for the reply. What I am doing is using a private property rather
than a local variable, something like:

private DataSet PageDataSet
{
get{return (DataSet)ViewState["VsDataSet"];}
set{ViewState["VsDataSet"]=value}
}

This make the persistence to viewstate transparent, but I wanted to be sure
I wasn't incurring any extra overhead.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top