ASP.NET Web 表单类中如何处理类字段?

发布于 2024-11-16 14:36:44 字数 437 浏览 6 评论 0原文

考虑一个带有关联代码文件 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗲 2024-11-23 14:36:44

不,这些对象将在页面生命周期结束时被处置。
如果您想将它们保留在 ViewState 中,您必须 将它们添加到其中

需要存储在 ViewState 中的对象必须是 可序列化

在 ViewState 中存储简单数据类型

与大多数类型的状态管理一样
ASP.NET,视图状态依赖于
字典集合,其中每个项目
使用唯一的字符串名称进行索引。
例如,考虑以下代码:

ViewState["ViewStateVariableName"] = 1;

这会将值 1 (或者更确切地说,
包含值 1) 的整数
进入 ViewState 集合并
给它一个描述性的名称
视图状态变量。如果有
目前没有具有该名称的项目
ViewStateVariable,一个新项目将是
自动添加。如果有
已经有一个项目在此索引
名称,它将被替换。

您可以在任何地方访问此变量
在页面/控件中
添加了视图状态变量。
检索值时,您可以使用
键名。

int number = (int) ViewState["ViewStateVariable"];

您还需要投射检索到的
值到适当的数据类型。
这是因为 ViewState
集合将所有项目存储为通用项
对象也给你
灵活地存储任何类型的基本
其中的数据类型。事实上,你可以
甚至将您的自定义对象存储在
查看状态。

在视图状态中存储对象

您可以在视图中存储自己的对象
状态就像存储一样容易
数字和字符串类型。然而,为了
将项目存储在视图状态中,ASP.NET
必须能够将其转换为
字节流,以便它可以
添加到隐藏的输入字段
页。这个过程称为
序列化。如果你的对象不是
可序列化(默认情况下它们
不是),你会收到一个错误
当您尝试放置它们时出现消息
处于视图状态。

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

Like most types of state management in
ASP.NET, view state relies on a
dictionary collection, where each item
is indexed with a unique string name.
For example, consider this code:

ViewState["ViewStateVariableName"] = 1;

This places the value 1 (or rather, an
integer that contains the value 1)
into the ViewState collection and
gives it the descriptive name
ViewStateVariable. If there is
currently no item with the name
ViewStateVariable, a new item will be
added automatically. If there is
already an item indexed under this
name, it will be replaced.

You can access this variable anywhere
within the page/control where the
viewstate variable has been added.
When retrieving a value, you use the
key name.

int number = (int) ViewState["ViewStateVariable"];

You also need to cast the retrieved
value to the appropriate data type.
This is because the ViewState
collection stores all items as generic
objects which also give you the
flexibility to store any type of basic
data types in it. In fact, you can
even store your custom objects in the
view state.

Storing Objects in View State

You can store your own objects in view
state just as easily as you store
numeric and string types. However, to
store an item in view state, ASP.NET
must be able to convert it into a
stream of bytes so that it can be
added to the hidden input field in the
page. This process is called
serialization. If your objects aren't
serializable (and by default they
aren't), you'll receive an error
message when you attempt to place them
in view state.

ι不睡觉的鱼゛ 2024-11-23 14:36:44

不,他们不是。它们不会被持久化以查看状态。

为了保留它们,您需要将它们设置为将其值存储在 ViewState 中的属性,如下所示:

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

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:

public string MyString
{
    get { return (string) ViewState["MyString"];
    set { ViewState["MyString"] = value; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文