类变量失去价值

use*_*084 0 c# asp.net class visual-studio-2008

这就是我的代码的样子.

我想知道为什么标签中的值变为空白或为空.

我想在获取数据时将值分配给用户名,并在按钮单击事件中使用它.

some1可以指导我这个,为什么会发生这种情况以及如何解决这个问题

我不想使用会话和静态.

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.
    }

}
Run Code Online (Sandbox Code Playgroud)

SLa*_*aks 8

您需要将值存储在ViewState或Session中.

通常,您需要了解ASP.Net生命周期和客户端 - 服务器交互模型.