class variable losing value

2019-08-30 06:06发布

问题:

This is how my code looks like.

I want to kno why is the value in the label coming as blank or null.

I want to assign the value to username at get data and use it at the button click event.

Can some1 guide me with this and why is it happening and how to solve this issue

I don't want to use a session and static.

namespace GUI
{
  public partial class Updatechild : System.Web.UI.Page
{
    string UserName;

   protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
        {
          //CALL A FUNCTION TO GET THE VALUE
          GetData();
        }
    }

   protected void GetData()
    {
      //VALUE assigned
       UserName= "USER1"
    }

   // button click event
   protected void Button_Click(object sender, EventArgs e)
    {
       Label.Text = UserName; //value comes as blank.
    }

}

回答1:

To answer the question of why this is happening: Basically each time the server handles a request from the browser for that page, the ASP.NET framework creates a new instance of your UpdateChild class.

In your scenario there are 2 separate requests happening: one when the page loads for the first time and another when the user clicks the button.

During the first request, since the request is not a postback, your UpdateChild class assigns a value to the UserName variable. However, since that instance of UpdateChild will be discarded once the request is done processing, the value assigned to that variable is lost. If you want to preserve this value, then you'll need to store it somewhere other than a class-level variable on your page. The most logical place would probably be in either ViewState or Session.

A simple solution to your problem is to change the declaration of UserName to something like the following:

    string UserName
    {
        get { return ViewState["UserName"] as string; }
        set { ViewState["UserName"] = value; }
    }


回答2:

You need to store the value in ViewState or Session.

In general, you need to understand the ASP.Net lifecycle and the client-server interaction model.



回答3:

The Button_Click event occurs on PostBack and GetData is not called.



回答4:

You should call GetData from the Button_Click method, also GetData(string node) requires a string. You are only calling it like so GetData()



回答5:

when you postback, the whole page is recreated, and since your UserName variable is also recreated, it will have its default value which is null. So you need to put this variable in ViewState after its value has been set.



回答6:

Your if Condition in the Page_Load event is the problem. When the first time page loads, this event gets fired and the UserName string gets assigned with the value from GetData(). But this value has to be persisted either using viewstate or session or static variable. When the user clicks the button, page_load event is fired but due to the If condition where u are checking for !IsPostBack the GetData() function doesn't assign the value to UserName variable. So your label will never get the Username value.

HTH