• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

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
0
sean-keys
Asked:
sean-keys
1 Solution
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now