Passing objects forth and back between ASP.NET pages.
-
Hi there. I'm currently creating a site which behaves like a kind of "wizard". So I need to pass objects forth and back between the pages in the wizard. Now this works fine passing objects forth, but I have a problem passing objects back or have the page remembering objects between post backs. Here's some example code:
private MyObject \_myObject; protected void Page\_Load(object sender, EventArgs e) { // Context.Items\["MyObject"\] has been set by another page. // An passed on using Server.Transfer() \_myObject = (MyObject)Context.Items\["MyObject"\]; \_myObject.MyString = "Hello"; } protected void Button1\_Click(object sender, EventArgs e) { MyObjects.SaveMyObject(\_myObject); }
Here's my problem: On my web form I have a button which calls "Button1_Click". Whenever this method is called I get a "NullReferenceException" from SaveMyObject - obviously because _myObject is null. But why? In the Page_Load method setting the MyString property to "Hello" works just fine. So my guess is that the page "forgets" the content of the context object between post backs. I guess I need to do something else. Is there some kind of solution to this problem? Best regards Soeren
-
Hi there. I'm currently creating a site which behaves like a kind of "wizard". So I need to pass objects forth and back between the pages in the wizard. Now this works fine passing objects forth, but I have a problem passing objects back or have the page remembering objects between post backs. Here's some example code:
private MyObject \_myObject; protected void Page\_Load(object sender, EventArgs e) { // Context.Items\["MyObject"\] has been set by another page. // An passed on using Server.Transfer() \_myObject = (MyObject)Context.Items\["MyObject"\]; \_myObject.MyString = "Hello"; } protected void Button1\_Click(object sender, EventArgs e) { MyObjects.SaveMyObject(\_myObject); }
Here's my problem: On my web form I have a button which calls "Button1_Click". Whenever this method is called I get a "NullReferenceException" from SaveMyObject - obviously because _myObject is null. But why? In the Page_Load method setting the MyString property to "Hello" works just fine. So my guess is that the page "forgets" the content of the context object between post backs. I guess I need to do something else. Is there some kind of solution to this problem? Best regards Soeren
-
Hi there. I'm currently creating a site which behaves like a kind of "wizard". So I need to pass objects forth and back between the pages in the wizard. Now this works fine passing objects forth, but I have a problem passing objects back or have the page remembering objects between post backs. Here's some example code:
private MyObject \_myObject; protected void Page\_Load(object sender, EventArgs e) { // Context.Items\["MyObject"\] has been set by another page. // An passed on using Server.Transfer() \_myObject = (MyObject)Context.Items\["MyObject"\]; \_myObject.MyString = "Hello"; } protected void Button1\_Click(object sender, EventArgs e) { MyObjects.SaveMyObject(\_myObject); }
Here's my problem: On my web form I have a button which calls "Button1_Click". Whenever this method is called I get a "NullReferenceException" from SaveMyObject - obviously because _myObject is null. But why? In the Page_Load method setting the MyString property to "Hello" works just fine. So my guess is that the page "forgets" the content of the context object between post backs. I guess I need to do something else. Is there some kind of solution to this problem? Best regards Soeren