Click here to Skip to main content
15,885,651 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have created a page UserControl.ascx, in Code behind file of UserControl.ascx and I have created an object of a class MyClass.cs,
Like this:
if(!Page.IsPostback) {
objMyClass= new MyClass();
}

But when I click on button of UserControl.ascx page and try to access objMyClass object then it set as null, so how to persist MyClass object when page PostBack?

One more thing is that MyClass is also resposnsible to create some other class object also.

So I have made MyClass Serializable and I have used ViewState to keep my objet persist, but it give the error
like this:
UserControl is not marked as serializable.


So I have made my UserControl as Serializable, but the problem is still
coming.
Can any one suggest me correct solution?
Posted
Updated 5-Oct-10 0:39am
v6
Comments
amit_83 5-Oct-10 9:35am    
[SerializationException: Type 'ASP.common_grid_commongridview_ascx' in Assembly 'App_Web_44142fbl, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.]
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) +9452985
System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) +247
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() +160
System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) +218
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) +388
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +444
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +133
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) +13
System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerBinaryWriter writer, Object value) +2937

[ArgumentException: Error serializing value 'Web.GridControl' of type 'Web.GridControl.']
System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerBinaryWriter writer, Object value) +3252
System.Web.UI.ObjectStateFormatter.Serialize(Stream outputStream, Object stateGraph) +116
System.Web.UI.ObjectStateFormatter.Serialize(Object stateGraph) +57
System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Serialize(Object state) +4
System.Web.UI.Util.SerializeWithAssert(IStateFormatter formatter, Object stateGraph) +37
System.Web.UI.HiddenFieldPageStatePersister.Save() +79
System.Web.UI.Page.SavePageStateToPersistenceMedium(Object state) +108
System.Web.UI.Page.SaveAllState() +315
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2839

Hi Amit,

You can persist class object using view state during post back.

C#
if(!Page.IsPostback) {
objMyClass= new MyClass();

ViewState["myclass"] = objMyClass;
}


public void button_click()
{
//Access value from viewstate
objMyClass= new MyClass();
objMyClass = (MyClass) ViewState["myclass"];
}



Please do let me know, if you have any doubt.

Please provide "Vote" if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen
 
Share this answer
 
Are trying to persist the user control itself? This should not be done, you don't mark an UserControl as serializable. You persist the data that is necessary for the control to recreate itself.

Do you understand the more you place in viewstate the larger your page will be when streamed to and from the client? A class with multiple child objects is usually not stored in viewstate.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900