HiddenFieldPageStatePersister Class

Definition

在 Web 客户端上以隐藏的 HTML 元素存储 ASP.NET 页面视图状态。Stores ASP.NET page view state on the Web client in a hidden HTML element.

public ref class HiddenFieldPageStatePersister : System::Web::UI::PageStatePersister
public class HiddenFieldPageStatePersister : System.Web.UI.PageStatePersister
type HiddenFieldPageStatePersister = class
    inherit PageStatePersister
Public Class HiddenFieldPageStatePersister
Inherits PageStatePersister
Inheritance
HiddenFieldPageStatePersister

Examples

下面的代码示例演示如何使用 SqlDataSource 控件将 ListBox 控件绑定到数据库中的数据。The following code example demonstrates how to bind a ListBox control to data in a database with a SqlDataSource control.

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          DataSourceMode="DataReader"
          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
          SelectCommand="SELECT LastName FROM Employees">
      </asp:SqlDataSource>

      <asp:ListBox
          id="ListBox1"
          runat="server"
          DataTextField="LastName"
          DataSourceID="SqlDataSource1">
      </asp:ListBox>

    </form>
  </body>
</html>
<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:SqlDataSource
          id="SqlDataSource1"
          runat="server"
          DataSourceMode="DataReader"
          ConnectionString="<%$ ConnectionStrings:MyNorthwind%>"
          SelectCommand="SELECT LastName FROM Employees">
      </asp:SqlDataSource>

      <asp:ListBox
          id="ListBox1"
          runat="server"
          DataTextField="LastName"
          DataSourceID="SqlDataSource1">
      </asp:ListBox>

    </form>
  </body>
</html>

查看示例页后,任何视图状态都由 HiddenFieldPageStatePersister 类存储在名为 "__VIEWSTATE" <hidden> HTML 元素中。When the example page is viewed, any view state is stored by the HiddenFieldPageStatePersister class in a <hidden> HTML element named "__VIEWSTATE".

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
<HTML>  
  <BODY>  
    <form name="_ctl0" method="post" action="sample.aspx" id="_ctl0">  
  
      <input type="hidden" name="__VIEWSTATE" value="MgAwADkANgAwADkAOQA3ADgAMQD/AQ8QBQoxMTU2NDc1NjE0EGRkFgICAQ9kFgICAw8PZBAVDQVhc2RhcwhCdWNoYW5hbghDYWxsYWhhbgdEYXZvbGlvCURvZHN3b3J0aAZGdWxsZXIES2luZwhLcmF3YXJpawlMZXZlcmxpbmcETWFyeQdQZWFjb2NrBlN1eWFtYQx0ZXN0IHJlZnJlc2gVDQVhc2RhcwhCdWNoYW5hbghDYWxsYWhhbgdEYXZvbGlvCURvZHN3b3J0aAZGdWxsZXIES2luZwhLcmF3YXJpawlMZXZlcmxpbmcETWFyeQdQZWFjb2NrBlN1eWFtYQx0ZXN0IHJlZnJlc2gUKgMNZ2dnZ2dnZ2dnZ2dnZ2RkZPdhi6U7jBPqVZbj0W8AYFSJ6oiq" />  
  
      <select size="4" name="ListBox1" id="ListBox1">      
        <option value="Buchanan">Buchanan</option>  
        <option value="Callahan">Callahan</option>  
        <option value="Davolio">Davolio</option>  
        <option value="Dodsworth">Dodsworth</option>  
        <option value="Fuller">Fuller</option>  
        <option value="King">King</option>  
        <option value="Leverling">Leverling</option>  
        <option value="Suyama">Suyama</option>  
      </select>  
  
    </form>  
  </BODY>  
</HTML>  

Remarks

ASP.NET 页可以将 Page 状态存储在本质上无状态 HTTP 请求和处理和服务任何网页所需的响应之间。ASP.NET pages can store Page state between the inherently stateless HTTP request and response required to process and serve any Web page. 此状态称为 "视图状态"。This state is called "view state". 用于存储页及其控件的视图状态的默认机制位于从 Web 服务器提供给客户端的页中包含的一个或多个 <hidden> HTML 字段中。The default mechanism used to store view state for a page and its controls is in one or more <hidden> HTML fields included in the page that is served to a client from a Web server. Page 类可以使用 HiddenFieldPageStatePersister 类来执行此操作。The Page class can use the HiddenFieldPageStatePersister class to do this work. HiddenFieldPageStatePersister 实现 PageStatePersister 类的 SaveLoad 方法,以便 Page 可以将状态存储在 Web 客户端上名为 "<hidden>" 的 __VIEWSTATE HTML 元素中。HiddenFieldPageStatePersister implements the Save and Load methods of the PageStatePersister class so that the Page can store state in a <hidden> HTML element named "__VIEWSTATE" on a Web client. 当客户端请求来自 Web 服务器(回发)的同一页面时,ASP.NET 将分析 <hidden> 元素,并将其转换为状态信息。When the client requests the same page from the Web server (a postback), the <hidden> element is parsed and converted to state information by ASP.NET.

如果请求的 Page 实例具有关联的 PageAdapter 类,则它将使用 GetStatePersister 方法从此适配器检索 PageStatePersister 对象。If the Page instance that is requested has an associated PageAdapter class, it retrieves the PageStatePersister object from this adapter using the GetStatePersister method. 适配器可以重写 GetStatePersister 方法以返回按特定客户端支持的某种方式存储页和控件状态的 PageStatePersister 对象。Adapters can override the GetStatePersister method to return a PageStatePersister object that stores page and control state in some way that a specific client supports. 例如,SessionPageStatePersister 类将页面和控件的状态存储在 Web 服务器上,而不是客户端上,以减少必须在内存和带宽可能有限的客户端上发送和存储的信息量。For example, the SessionPageStatePersister class stores page and controls' state on the Web server, instead of the client, to decrease the amount of information that must be sent and stored on a client that has potentially limited memory and bandwidth.

可以通过 MaxPageStateFieldLength 属性或通过 MaxPageStateFieldLength 配置设置,设置 "_VIEWSTATE" <hidden> HTML 元素的最大大小。The maximum size of the "_VIEWSTATE" <hidden> HTML element can be set through the MaxPageStateFieldLength property or through the MaxPageStateFieldLength configuration setting. 如果设置了此值,并且 "_VIEWSTATE" 元素的长度超出了最大长度,则内容将被划分为多个 "_VIEWSTATE" 元素。When this value is set and the length of the "_VIEWSTATE" element would exceed the maximum length, the content is divided into multiple "_VIEWSTATE" elements.

Constructors

HiddenFieldPageStatePersister(Page)

初始化 HiddenFieldPageStatePersister 类的新实例。Initializes a new instance of the HiddenFieldPageStatePersister class.

Properties

ControlState

获取或设置一个对象,该对象表示当前 Page 对象包含的控件用于跨 Web 服务器的 HTTP 请求保留的数据。Gets or sets an object that represents the data that controls contained by the current Page object use to persist across HTTP requests to the Web server.

(Inherited from PageStatePersister)
Page

获取或设置为其创建视图状态持久性机制的 Page 对象。Gets or sets the Page object that the view state persistence mechanism is created for.

(Inherited from PageStatePersister)
StateFormatter

获取一个 IStateFormatter 对象,该对象在对 ViewStateControlState 方法的调用期间用于对包含在 Save()Load() 属性中的状态信息进行序列化和反序列化。Gets an IStateFormatter object that is used to serialize and deserialize the state information contained in the ViewState and ControlState properties during calls to the Save() and Load() methods.

(Inherited from PageStatePersister)
ViewState

获取或设置一个对象,该对象表示当前 Page 对象包含的控件用于跨 Web 服务器的 HTTP 请求保留的数据。Gets or sets an object that represents the data that controls contained by the current Page object use to persist across HTTP requests to the Web server.

(Inherited from PageStatePersister)

Methods

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
Load()

Page 对象初始化其控件层次结构时,反序列化并加载 HttpRequest 对象中的持久化状态信息。Deserializes and loads persisted state information from an HttpRequest object when a Page object initializes its control hierarchy.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Save()

序列化 ViewStateControlState 属性中包含的任何对象状态,并将状态写入响应流中。Serializes any object state contained in the ViewState or ControlState property and writes the state to the response stream.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also