ViewState storing custom class
-
How do I store a custom object in a ViewState. I know that viewstates do not support XmlSerialization. I currently have a class that is using XmlSerialization and all hell is breaking lose when trying to set a viewstate entry to that value.
ViewState is actually a name / value collection. You can't store a class in ViewState. You can store a serializable object in Session though.
I didn't get any requirements for the signature
-
ViewState is actually a name / value collection. You can't store a class in ViewState. You can store a serializable object in Session though.
I didn't get any requirements for the signature
-
So is that what you are doing? In my 8 years of doing asp.net I have never once tried to store an object in viewstate. Can ask why you want to store an object in viewstate?
I didn't get any requirements for the signature
-
So is that what you are doing? In my 8 years of doing asp.net I have never once tried to store an object in viewstate. Can ask why you want to store an object in viewstate?
I didn't get any requirements for the signature
-
Well, if you have a base page class or a master page. Store the object in session. The reason for the base page class or master page is that you can check what aspx page is being loaded and set the session object to null if it is no longer needed. So it will behave like viewstate but you won't have all that data traveling back and forth to the client.
I didn't get any requirements for the signature
-
Well, if you have a base page class or a master page. Store the object in session. The reason for the base page class or master page is that you can check what aspx page is being loaded and set the session object to null if it is no longer needed. So it will behave like viewstate but you won't have all that data traveling back and forth to the client.
I didn't get any requirements for the signature
In the absence of a master page or base class you can also use the global.asax to manage the session objects.
-
In the absence of a master page or base class you can also use the global.asax to manage the session objects.
Thanks for the tip, I never thought of that. Does the Application_BeginRequest event fire on every post back?
I didn't get any requirements for the signature
-
Thanks for the tip, I never thought of that. Does the Application_BeginRequest event fire on every post back?
I didn't get any requirements for the signature
I think so but the way I usually do it is to wire up an event to page PreInit in Application_PreRequestHandlerExecute. It might not be the best way to do it (so I'm open to suggestions) but it works a treat.
-
Well, if you have a base page class or a master page. Store the object in session. The reason for the base page class or master page is that you can check what aspx page is being loaded and set the session object to null if it is no longer needed. So it will behave like viewstate but you won't have all that data traveling back and forth to the client.
I didn't get any requirements for the signature
-
I wanted to avoid using session vars, but I guess that is my only option at the moment. Thanks!
Just one more tip for you. Create a static class to hold all your session objects. For example
public static class SessionItems
{
public static YourObjectType YourObject
{
get
{
Object result = HttpContext.Current.Session["YourObject"];
if (result == null)
return null;
else
return (YourObjectType)result;
}
set { HttpContext.Current.Session["YourObject"] = value; }
}
}It makes it easier to manager your session objects this way, plus it prevents bugs from trying to access a session object that does not exist. Also prevents bugs that can occur from mispellings
I didn't get any requirements for the signature