Saving objects in ViewState

  • Thread starter Thread starter Michael Albanese
  • Start date Start date
M

Michael Albanese

Can you save objects a (custom class) to Viewstate and
then get them on a later page??

I have a custom class for an ASP.Net application that I
would like to make available across several web pages.

For example:

page1.aspx accepts a users's name, age and gender,
instantiated the new class and redirects to page2.aspx

page2.aspx accesses the custom class for the information
and takes some action based on defined business rules.

My application needs access to a defined set of data
across a series of five sepparate web pages, and i am
looking for the best way to do this.

Thanks,

Michael Albanese
 
Michael Albanese said:
Can you save objects a (custom class) to Viewstate and
then get them on a later page??

I have a custom class for an ASP.Net application that I
would like to make available across several web pages.

Why not use session state?

-- Alan
 
So long as your class can be serialized, you can put it in
Viewstate. However, Viewstate is not really optimized for
storing objects and session state is generally considered
a better solution.
 
Hi Michael,

Thanks for your post. I agree with previous replies from Christopher and
Alan. In MSDN, there is code snippet which demonstrates saving/restoring a
dataset to/from view state.

//------------------code snippet--------------------
sqlDataAdapter1.Fill(dSet);
System.IO.StringWriter sw = new System.IO.StringWriter();

// Write the DataSet to the ViewState property.
dSet.WriteXml(sw);
ViewState["dSet"] = sw.ToString();

//**********************************************

if (Page.IsPostBack)
{
System.IO.StringReader sr =
new System.IO.StringReader((string)(ViewState["dSet"]));
dSet.ReadXml(sr);
}
//---------------------end of-------------------------------

Please kindly note that Session state and Application state are on the
Server side, while View state is on the client side. I strongly recommend
you the following MSDN article:

State Management Recommendations
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/
vbconChoosingServerStateOption.asp

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks for the feedback.

I have a working version of the app that uses session state and that
will be fine i guess.

I have printed the MSDN article and will read it today.

Thanks again

Michael
 
Back
Top