类初始化的变量就失去了previous值与网页加载 [英] variable initialized in class loses its previous value with the page loading

查看:117
本文介绍了类初始化的变量就失去了previous值与网页加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经声明了一个字符串变量测试与喜。每次我点击Button1的时间,我希望这个测试将与其previous价值附加。但我注意到,它就失去了previous值单击该按钮,加载页面时。这就是我点击它时,它都会有其为日冰的文字。我期望在接下来的点击等等。hihihihi。什么跟下面的code,这里的问题?

 公共部分类_Default:System.Web.UI.Page
{    字符串测试=HI;    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {    }
    保护无效的button1_Click(对象发件人,EventArgs的发送)
    {
        测试+ =测试;
        Button1.Text =测试;
    }
}


解决方案

没有,那是没有办法的办法asp.net的作品。如果你需要的行为,你应该这样做:

 公共字符串测试{
  获得{
    返回(串)的ViewState [测试]? HI;
  }
  集合{
    的ViewState [测试] =值;
  }
}

在ASP.NET向服务器发送一个请求,你的类的一个新版本被实例化。如果你需要得到的状态,你需要使用ViewState的(这在每个页面保存在浏览器中的隐藏字段,并与每个请求发送,因此状态保存的 的),或者你可以使用SessionState会这是每个用户保存的状态。 SessionState会默认保存在存储器中。所以,如果你重新启动IIS,这种状态会自行消失。需要注意的是视图状态的国家将不会消失,如果你重置IIS(因为它是由浏览器发送)。您也可以使用缓存这再次,被保存在内存中。这种状态是的应用程序的所有的用户。有关重置IIS的规则也同样适用。最后,你可以让你的静态变量。正如我所说的,每次发出请求类的新版本时被实例化。当然,静态变量不是实例变量,因此静态变量的状态保存在回传也。有关IIS同样的规则复位适用于静态变量缓存和会话。

I've declared a String variable test with "hi". every time I click on Button1, I expect that test will be appended with its previous value. But I have noticed that it loses its previous value when the button is clicked and the page is reloaded. That is every time I click it, it has its text as "hihi". I expect "hihihihi" on the next click and so on. What's the problem here with the code below?

public partial class _Default : System.Web.UI.Page
{

    String test = "hi";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        test += test;
        Button1.Text = test;
    }
}

解决方案

No, that's not the way asp.net works. If you need that behavior you should do this:

public string test {
  get {
    return (string) ViewState["test"] ?? "hi";
  }
  set {
    ViewState["test"] = value;
  }
}

When ASP.NET sends a request to the server, a new version of your class is instantiated. If you need to get the state, you need to use ViewState (which is saved in a hidden field in the browser and sent with every request, and therefore state saved per page), or you can use SessionState which is a state saved per user. SessionState by default is saved in memory. So, if you restart IIS, this state will go away. Note that viewstate's state will NOT go away if you reset IIS (since it's being sent by the browser). You can also use the Cache which again, is saved in memory. This state is for all users of your application. The same rules about resetting IIS apply. Finally, you could make your variable static. As I said, every time a request is made a new version of your class is instantiated. Of course, static variables are not instance variables, so the state of a static variable is saved across postbacks as well. The same rules about IIS reset apply to static variables as Cache and Session.

这篇关于类初始化的变量就失去了previous值与网页加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆