String in web form keeps going to null c#

I have this code to make a string called MODE.

          public class customers : System.Web.UI.Page

                         public string MODE="UPDATE";

Then I have a method to change the value of MODE

private void ChangeCustomerMode(string Mode)
      {
            string Change = Mode;
                  if (Change == "ADD")
                        {
                              MODE="NEW";     //I watch it set to "NEW"
                              btn_add_customer.Visible = false;
                              btn_update_customer.Text = "Add New Customer";
                        }
                  if (Change == "UPDATE")
                  {
                        MODE="UPDATE";
                        btn_add_customer.Visible = true;
                        btn_update_customer.Text = "Update Selected Customer";
                  }

}

At this point the debugger says the value is now "NEW"

But then I call this method

private void btn_update_customer_Click(object sender, System.EventArgs e)
            {
                  StoreCustomerInfo();
             }

private void StoreCustomerInfo()
            {                                                      
                        
                  if (MODE == "NEW")                           // right here it reads as "UPDATE"
                  {
                        AddCustomer();
                  }
                  else
                  {
                  }

            }

and the MODE is back to "UPDATE"

What am I doing wrong??????    

Thanks,
Sean
sean-keysAsked:
Who is Participating?
 
BurntSkyCommented:
It's a state problem.  Unlike WindowsForms applications, web applications are stateless.  Everytime the page gets loaded all the variables are reset to their default values (technically, they aren't even "reset" because they've never even been initialized in the first place.)

Anyway, if you want to retain the value of variables between postback you can store them in ViewState or Session.

ViewState["MODE"] = this.MODE;  // sets it
this.MODE = ViewState["MODE"].ToString(); // retrieves it (be sure to watch for nulls)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.