String in web form keeps going to null c#

Posted on 2005-05-02
Last Modified: 2010-04-07
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")
                        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)

private void StoreCustomerInfo()
                  if (MODE == "NEW")                           // right here it reads as "UPDATE"


and the MODE is back to "UPDATE"

What am I doing wrong??????    

Question by:sean-keys
    1 Comment
    LVL 9

    Accepted Solution

    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)

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    This video discusses moving either the default database or any database to a new volume.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now