ASP.NET Web 表单类中如何处理类字段?
考虑一个带有关联代码文件 (aspx.cs) 的 Web 表单 (aspx)。
例如,在代码文件中,我们有以下内容:
public partial class MyPage : System.Web.UI.Page {
int myint;
string mystring;
public void Page_Load(object sender, EventArgs e) {
...
if (!this.IsPostBack) {
this.myint = 2;
this.mystring = "Hello";
}
...
}
}
我可以期望每次回发页面时都有这两个变量吗?我的意思是:页面类字段是否保留为 Web 表单状态的一部分?
谢谢
Consider a web form (aspx) wth an associated codefile (aspx.cs).
In the codefile, for instance, we have the following:
public partial class MyPage : System.Web.UI.Page {
int myint;
string mystring;
public void Page_Load(object sender, EventArgs e) {
...
if (!this.IsPostBack) {
this.myint = 2;
this.mystring = "Hello";
}
...
}
}
Can I expect to have the two variables everytime the page is posted back? I mean: are page class fields kept as part of the web form state?
Thankyou
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,这些对象将在页面生命周期结束时被处置。
如果您想将它们保留在 ViewState 中,您必须 将它们添加到其中。
需要存储在 ViewState 中的对象必须是 可序列化。
在 ViewState 中存储简单数据类型
在视图状态中存储对象
No, these objects would be disposed at the end of the Page-Lifecycle.
If you want to persist them in ViewState you have to add them to it.
Objects that need to be stored in ViewState must be serializable.
Storing Simple Data Types in the ViewState
Storing Objects in View State
不,他们不是。它们不会被持久化以查看状态。
为了保留它们,您需要将它们设置为将其值存储在 ViewState 中的属性,如下所示:
No, they are not. They are not persisted to viewstate.
In order to persist them, you need to make them properties that store their values in ViewState, like so: