ViewState question - for tech-heads only

  • Thread starter Thread starter Gandalf
  • Start date Start date
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
 
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
 
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.
 
One thing I would do: In your get method, include a check to see whether the
ViewState value is null, as it will be until you assign it. Something like:

get
{
if (ViewState["VsDataSet"] == null) return null;
return (DataSet)ViewState["VsDataSet"];
}

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Gandalf said:
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.
--

Teemu Keiski said:
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
 
Thanks. I was trying to be concise. What I usually do is:

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


--

Kevin Spencer said:
One thing I would do: In your get method, include a check to see whether the
ViewState value is null, as it will be until you assign it. Something like:

get
{
if (ViewState["VsDataSet"] == null) return null;
return (DataSet)ViewState["VsDataSet"];
}

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Gandalf said:
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.
--

Teemu Keiski said:
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
or
 
BTW, it's OK to cast a null, so your IF statement is redundant.

--

Kevin Spencer said:
One thing I would do: In your get method, include a check to see whether the
ViewState value is null, as it will be until you assign it. Something like:

get
{
if (ViewState["VsDataSet"] == null) return null;
return (DataSet)ViewState["VsDataSet"];
}

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Gandalf said:
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.
--

Teemu Keiski said:
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
or
 
Back
Top