I try to store some values in a viewstate in a class, but I could not make it work. Can someone show me the correct way to store a value (Datatable) in a viewstate in a class?
It works okay within a webpage, but I couldn't make it work with a class.
The viewstate belongs to the page/control, so there's no such thing as "viewstate in a class" unless the class is a web page/control. Of course, you can access the viewstate from the class, if you give it a reference to the page/control or the viewstate
itself.
public class SomeClass
{
IDictionary _stateBag;
public SomeClass(IDictionary stateBag)
{
_stateBag=stateBag;
}
public void DoSomething()
{
DataTable dt=GetDataTable();
_stateBag["theDataTable"]=dt;
}
}
Session can solve the problem, but it use alot of server resource while viewstate just throw everything to client.
FYI, viewstate isn't exactly lean either. The decryption/deserialization phase consumes server resources, and the request/response size increases, which means more bandwidth usage for both client and server.
-- "Mark As Answer" if my reply helped you --
Marked as answer by Ivan Xin - MSFT on Apr 04, 2008 03:03 AM
vinhwsu
Member
152 Points
73 Posts
How to store viewstate in a class c#
Apr 02, 2008 08:23 PM|LINK
Hello all,
I try to store some values in a viewstate in a class, but I could not make it work. Can someone show me the correct way to store a value (Datatable) in a viewstate in a class?
It works okay within a webpage, but I couldn't make it work with a class.
Thanks
anas
All-Star
73649 Points
7914 Posts
Moderator
Re: How to store viewstate in a class c#
Apr 02, 2008 08:39 PM|LINK
are you inheriting from contrl class ?
you can't use the view state in a class that dont inherits from "System.Web.UI.Control"
crfenix
Participant
853 Points
168 Posts
Re: How to store viewstate in a class c#
Apr 02, 2008 08:40 PM|LINK
Hi!
You cannot use the viewstate outside of web controls. You can use the session that could be the same for your objetive
HttpContext.Current.Session[
"MyValue"] = myValue;Claudio Redi
Simplicity: "the art of maximizing the amount of work not done."
gunteman
All-Star
22406 Points
3305 Posts
Re: How to store viewstate in a class c#
Apr 02, 2008 08:42 PM|LINK
The viewstate belongs to the page/control, so there's no such thing as "viewstate in a class" unless the class is a web page/control. Of course, you can access the viewstate from the class, if you give it a reference to the page/control or the viewstate itself.
public class SomeClass
{
IDictionary _stateBag;
public SomeClass(IDictionary stateBag)
{
_stateBag=stateBag;
}
public void DoSomething()
{
DataTable dt=GetDataTable();
_stateBag["theDataTable"]=dt;
}
}
and instantiate it on a page
SomeClass c=new SomeClass(this.ViewState);
vinhwsu
Member
152 Points
73 Posts
Re: How to store viewstate in a class c#
Apr 02, 2008 09:17 PM|LINK
Thanks all,
Session can solve the problem, but it use alot of server resource while viewstate just throw everything to client.
I just did like what Qunteman showed, it works great
gunteman
All-Star
22406 Points
3305 Posts
Re: How to store viewstate in a class c#
Apr 02, 2008 10:04 PM|LINK
FYI, viewstate isn't exactly lean either. The decryption/deserialization phase consumes server resources, and the request/response size increases, which means more bandwidth usage for both client and server.
anas
All-Star
73649 Points
7914 Posts
Moderator
Re: How to store viewstate in a class c#
Apr 02, 2008 11:36 PM|LINK
Just one more note ,
you can't save the DataTable in the ViewState because the DataTable is not Serializable , Instead you can use the DataSet ,
And as mentioned ,its not effecient to store a complex type like the dataset ...
the Viewstate is optimized for the simple types (primitives , and arrays ..) , but not for this complex types ...
thanks