0

I have a simple button:

<asp:Button ID="mybtn" runat="server" Text="My Btn"  onclick="mybtn_Click" />

And code behind:

protected MyCustomObject myObject;

private void Page_Load(object sender, System.EventArgs e)
{
  if (!IsPostBack)
  {
    this.myObject = new MyCustomObject();
  }
}

protected void mybtn_Click(object sender, EventArgs e)
{
  this.myObject.step(1);
}

protected void anotherbtn_Click(object sender, EventArgs e)
{
  this.myObject.step(2);
}

The problem: when click, the Page_Load is called and this.myObject is null.

The goal is to call anotherbtn_Click after call mybtn_Click with the same object instance.

How to keep object's context?

3 Answers 3

1

You can not assume that an instance of the Page class is kept for each "user"; therefore you can not use class variables as in your example. In order to store information across multiple requests, you can use the Session Context. See the following link for more information: http://msdn.microsoft.com/en-us/library/ms178581%28v=vs.100%29.aspx

In your case you could store the MyCustomObject instance in the session state. Example

private void Page_Load(object sender, System.EventArgs e)
{
  if (!IsPostBack)
  {
    Session["MyObject"] = new MyCustomObject();
  }
}

protected void mybtn_Click(object sender, EventArgs e)
{
  MyCustomObject myObject = Session["MyObject"] as MyCustomObject;
  myObject.step(1);
}

protected void anotherbtn_Click(object sender, EventArgs e)
{
  MyCustomObject myObject = Session["MyObject"] as MyCustomObject;
  myObject.step(2);
}
Sign up to request clarification or add additional context in comments.

Comments

1

The web is stateless. You always needs to create a new object for every request/post.

This means when every time your page is accessed , the new page instance will be created and served for it.

There are few ways you can solve your problem by using Session or serialize the object and keep it in ViewState

 if (!IsPostBack)
 {
   this.myObject = new MyCustomObject();
   Sesssion["oldObject"]=this.myObject;
 }

protected void mybtn_Click(object sender, EventArgs e)
{
      if(session["oldObject"]!=null)
       {
        this.myObject=session["oldObject"] as MyCustomObject;
        this.myObject.step(1);
       }
}

protected void anotherbtn_Click(object sender, EventArgs e)
{
  if(session["oldObject"]!=null)
   {
    this.myObject=session["oldObject"] as MyCustomObject;
    this.myObject.step(2);
   }
}

Comments

0

this.myObject is null because you explicitly do not recreate it in Page_Load on postback.

Depending on your needs, you can either

  • remove the if (!IsPostBack) check so that a new instance is created on each load; or

  • put the instance in the session after it is created if you need to keep the same instance around between postbacks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.