如何在Global.asax之外更改应用程序变量?

时间:2012-11-30 05:24:56

标签: c# asp.net web-applications

您好我正在尝试在Global.asax之外更改Application变量的变量,但之前的数据似乎再次出现。有什么办法可以改变变量的值或重置它。谢谢...

In Global.asax :

 void Application_Start(object sender, EventArgs e)
 {
    Application["abc"] = "";
 }

In  aspx.cs page :

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      ViewState["countmsg"] = 0;
   }
}

protected void Button1_Click(object sender, EventArgs e)
{
   ViewState["countmsg"] = Convert.ToInt32(ViewState["countmsg"]) +1;
   Application["abc"] = Application["abc"] + "hello" + Environment.NewLine;
   string str = Application["abc"].ToString();
     if (Convert.ToInt32(ViewState["countmsg"]) >= 4)
     {
        Application["abc"] = "";    // here the reset is not working
        ViewState["countmsg"] = 0;
     }
}

1 个答案:

答案 0 :(得分:0)

protected void Button1_Click(object sender, EventArgs e)
{
  ViewState["countmsg"] = Convert.ToInt32(ViewState["countmsg"]) +1;
  Application["abc"] = Application["abc"] + "hello" + Environment.NewLine;
  string str = Application["abc"].ToString();

   if (Convert.ToInt32(ViewState["countmsg"]) >= 4)
   {
    Application.Lock();
    Application["abc"] = "";    // now its working
    Application.UnLock();  
    ViewState["countmsg"] = 0;
  }
}