Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

I have created a page WebForm.aspx and in Code behind file of WebForm.aspx and I have created an object of a class MyClass.cs.

Like
if(!Page.IsPostback) {
    objMyClass= new MyClass();
}
.

However when I click on button of WebForm.aspx page and try to access objMyClass object then it is set as null.

How to persist MyClass object when page PostBack?

One more thing is that MyClass is also resposnsible to create some other class object also.
Posted
Updated 6-Oct-10 21:39pm
v3
Comments
Dalek Dave 7-Oct-10 3:39am    
Edited for Grammar and Readability.

you can serialize the class and can store it in viewState. so that viewState will preserve object during Page Postback.


also you can try to make static property and can strore assign it to property.
 
Share this answer
 
Save your object in ViewState

make sure your class is serializable..

ViewState["__MyClass"] = objMyClass;

now on after page is loaded.. access viewstate to get your object back

MyClass objMyClass = ViewState["__MyClass"] as MyClass;
 
Share this answer
 
v2
All you need to do is write
Session['MyObject']=objMyClass;
Then retrieve it from Session['MyObject'] which is persistent nad I beleive no serialization is required
 
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