请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
先前版本
全部折叠/全部展开 全部折叠
此页面仅适用于
Microsoft Visual Studio 2005/.NET Framework 2.0

同时提供下列产品的其他版本:
.NET Framework 类库
HttpSessionState 类

提供对会话状态值以及会话级别设置和生存期管理方法的访问。

命名空间:System.Web.SessionState
程序集:System.Web(在 system.web.dll 中)

Visual Basic(声明)
Public NotInheritable Class HttpSessionState
    Implements ICollection, IEnumerable
Visual Basic(用法)
Dim instance As HttpSessionState
C#
public sealed class HttpSessionState : ICollection, IEnumerable
C++
public ref class HttpSessionState sealed : ICollection, IEnumerable
J#
public final class HttpSessionState implements ICollection, IEnumerable
JScript
public final class HttpSessionState implements ICollection, IEnumerable

ASP.NET 提供了会话状态管理,使您可以根据多种请求存储与唯一浏览器会话相关联的信息。您可以存储由键名或数字索引引用的值的集合。您可以使用 HttpSessionState 类访问会话值和功能,该类可通过当前 HttpContextSession 属性或 PageSession 属性进行访问。

会话数据通过唯一标识符与特定浏览器会话相互关联。默认条件下,该标识符存储在浏览器的不过期会话 Cookie 中,但是您也可以在应用程序配置的 sessionState 元素中,将 cookieless 属性设置为 trueUseUri,以此方法配置应用程序,将会话标识符存储在 URL 中。您可以通过指定 cookieless 属性的 UseDeviceProfile 值,让 ASP.NET 确定浏览器是否支持 Cookie。您也可以为 cookieless 属性指定 AutoDetect 值,让 ASP.NET 确定是否为浏览器启用 Cookie。如果指定 UseDeviceProfile 后浏览器支持 Cookie,或指定 AutoDetect 后浏览器启用了 Cookie,则会话标识符将存储在 Cookie 中;否则它将存储在 URL 中。

第一次请求过程中会话将会启动,并且在达到 Timeout 属性中指定的分钟数之前如果浏览器未发送新的请求,当前的会话值将持续有效。新会话开始后将引发会话 Start 事件。会话启动时您可以使用此事件执行任何其他工作,例如设置默认会话值。如果会话超时,将调用 Abandon 方法,或者关闭 ASP.NET 应用程序,此时将引发会话 End 事件。您可以使用此事件执行任何必要的清理操作。仅当会话状态 mode 被设置为 InProc 时,才引发 End 事件。

若要提高性能,只有数据确实存储在 Session 对象中后,使用 Cookie 的会话才会分配会话存储。有关更多信息,请参见 SessionID 属性。

会话状态的持续性不会跨越 ASP.NET 应用程序的边界。如果浏览器定位到另一应用程序,则当前的会话信息对新应用程序不再有效。

默认条件下,会话值存储在 Web 服务器的内存中。您也可以把会话值存储在 SQL Server 数据库、ASP.NET 状态服务器或自定义服务器中。一旦 ASP.NET 或 IIS 进程或 ASP.NET 应用程序重新启动,这种举措可以保存会话值,并且它还可以使会话值在网络场的所有服务器间可用。要配置这种行为,请在应用程序配置的 sessionState 元素中把 mode 属性设置为有效的 SessionStateMode 值。有关更多信息,请参见 会话状态模式

会话状态的替代项包括应用程序状态(请参见 Application 属性)和 ASP.NET 缓存(请参见 System.Web.Caching 命名空间),它们存储可由 ASP.NET 应用程序的所有用户访问的变量;ASP.NET 配置文件(请参见 System.Web.Profile 命名空间),它将用户值保持在数据存储区中,而不使用超时功能设置到期时间;ASP.NET System.Web.UI.WebControls,它将控件的值保持在 ViewState 中;CookiesQueryString 属性;HTML 窗体上的字段,可使用 Form 集合通过 HTTP POST 使用这些字段。有关会话状态与其他状态管理替代项之间的区别的更多详细信息,请参见 ASP.NET 状态管理建议

TopicLocation
如何:保存会话状态中的值生成 ASP .NET Web 应用程序
如何:保存会话状态中的值在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:演示会话状态存储提供程序生成 ASP .NET Web 应用程序
如何:演示会话状态存储提供程序在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:读取会话状态中的值生成 ASP .NET Web 应用程序
如何:读取会话状态中的值在 Visual Studio 中生成 ASP .NET Web 应用程序

下面的代码示例可以设置和检索会话状态的值。

C#
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  public void Page_Load(object sender, EventArgs args)
  {
    if (!IsPostBack)
    {
      if (Session["address"] == null)
      {
        enterUserInfoPanel.Visible = true;
        userInfoPanel.Visible = false;
      }
      else
      {
        enterUserInfoPanel.Visible = false;
        userInfoPanel.Visible = true;

        SetLabels();
      }
    }
  }

  protected void SetLabels()
  {
    firstNameLabel.Text = Session["firstName"].ToString();
    lastNameLabel.Text = Session["lastName"].ToString();
    addressLabel.Text = Session["address"].ToString();
    cityLabel.Text = Session["city"].ToString();
    stateOrProvinceLabel.Text = Session["stateOrProvince"].ToString();
    zipCodeLabel.Text = Session["zipCode"].ToString();
    countryLabel.Text = Session["country"].ToString();
  }

  protected void EnterInfoButton_OnClick(object sender, EventArgs e)
  {
    Session["firstName"] = Server.HtmlEncode(firstNameTextBox.Text);
    Session["lastName"] = Server.HtmlEncode(lastNameTextBox.Text);
    Session["address"] = Server.HtmlEncode(addressTextBox.Text);
    Session["city"] = Server.HtmlEncode(cityTextBox.Text);
    Session["stateOrProvince"] = Server.HtmlEncode(stateOrProvinceTextBox.Text);
    Session["zipCode"] = Server.HtmlEncode(zipCodeTextBox.Text);
    Session["country"] = Server.HtmlEncode(countryTextBox.Text);

    enterUserInfoPanel.Visible = false;
    userInfoPanel.Visible = true;

    SetLabels();
  }

  protected void ChangeInfoButton_OnClick(object sender, EventArgs args)
  {
    enterUserInfoPanel.Visible = true;
    userInfoPanel.Visible = true;
  }
</script>

<html >
<head id="Head1" runat="server">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>User Information</title>
</head>
<body>
  <form id="form1" runat="server">
    <h3>
      User information</h3>
    <asp:Label ID="Msg" ForeColor="maroon" runat="server" /><br />
    <asp:Panel ID="enterUserInfoPanel" runat="server">
      <table cellpadding="3" border="0">
        <tr>
          <td>
            First name:</td>
          <td>
            <asp:TextBox ID="firstNameTextBox" runat="server" /></td>
        </tr>
        <tr>
          <td>
            Last name:</td>
          <td>
            <asp:TextBox ID="lastNameTextBox" runat="server" /></td>
        </tr>
        <tr>
          <td>
            Address:</td>
          <td>
            <asp:TextBox ID="addressTextBox" runat="server" /></td>
        </tr>
        <tr>
          <td>
            City:</td>
          <td>
            <asp:TextBox ID="cityTextBox" runat="server" /></td>
        </tr>
        <tr>
          <td>
            State or Province:</td>
          <td>
            <asp:TextBox ID="stateOrProvinceTextBox" runat="server" /></td>
        </tr>
        <tr>
          <td>
            Zip Code/Postal Code:</td>
          <td>
            <asp:TextBox ID="zipCodeTextBox" runat="server" /></td>
        </tr>
        <tr>
          <td>
            Country:</td>
          <td>
            <asp:TextBox ID="countryTextBox" runat="server" /></td>
        </tr>
        <tr>
          <td>
            &nbsp;</td>
          <td>
            <asp:Button ID="enterInfoButton" runat="server" Text="Enter user information" OnClick="EnterInfoButton_OnClick" /></td>
        </tr>
      </table>
    </asp:Panel>
    <asp:Panel ID="userInfoPanel" runat="server">
      <table cellpadding="3" border="0">
        <tr>
          <td>
            Name:</td>
          <td>
            <asp:Label ID="firstNameLabel" runat="server" />
            <asp:Label ID="lastNameLabel" runat="server" />
          </td>
        </tr>
        <tr>
          <td valign="top">
            address:</td>
          <td>
            <asp:Label ID="addressLabel" runat="server" /><br />
            <asp:Label ID="cityLabel" runat="server" />,
            <asp:Label ID="stateOrProvinceLabel" runat="server" />
            <asp:Label ID="zipCodeLabel" runat="server" /><br />
            <asp:Label ID="countryLabel" runat="server" />
          </td>
        </tr>
        <tr>
          <td>
            &nbsp;</td>
          <td>
            <asp:Button ID="changeInfoButton" runat="server" Text="Change user information" OnClick="ChangeInfoButton_OnClick" /></td>
        </tr>
      </table>
    </asp:Panel>
  </form>
</body>
</html>
System.Object
  System.Web.SessionState.HttpSessionState
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

.NET Framework

受以下版本支持:2.0、1.1、1.0
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利 | 商标 | 隐私权声明
Page view tracker