ASP.NET Tutorial/Sessions/Session class

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Session.Abandon()

<%@ Page %>
<script language="C#" runat="server">
private void Page_PreRender(object sender, EventArgs e)
{
  if(Session["UserName"] != null) UserNameLabel.Text = Session["UserName"].ToString();
}
private void SaveButton_Click(object sender, System.EventArgs e)
{
  Session["UserName"] = UserNameTextBox.Text;
}
private void AbandonButton_Click(object sender, System.EventArgs e)
{
  Session.Abandon();
}
</script>
<html>
  <body>
    <form id="form1" method="post" runat="server">
      
        Update UserName in Session: <asp:TextBox id="UserNameTextBox" runat="server"></asp:TextBox>
        <asp:Button id="SaveButton" runat="server" Text="Save" OnClick="SaveButton_Click"></asp:Button>
      
        Current Session Contents: <br/>
        <asp:Label ID="UserNameLabel" Runat="server" EnableViewState="False"></asp:Label>
        <br/>
        <asp:Button id="RefreshButton" runat="server" Text="Refresh Without Saving"></asp:Button>
        <br/>
        Abandon Session - takes effect on *next* request since current request is still referring to Session.<br/>
        <asp:Button id="AbandonButton" runat="server" Text="Abandon Session" OnClick="AbandonButton_Click"></asp:Button>
      
      <a href="Default.aspx">VB Version - same session</a>
    </form>
  </body>
</html>


Session.CodePage

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            Message.Text = "Current Code Page is: " & Session.CodePage
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>


Session.Contents.RemoveAll()

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            Session.Contents.RemoveAll()
            Message.Text = "Removed all items from current Session."
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>


Session.CopyTo

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            If Session.Count > 0 Then
               Dim myArray As Array = Array.CreateInstance(GetType(Object), _
                  Session.Count)
               Session.CopyTo(myArray, 0)
               Message.Text = "The first item in the array is: " & myArray(0)
            End If
         End Sub
      </script>
   </head>
<body>
   <form runat="server">
      <asp:label id="Message" runat="server"/>
   </form>
</body>
</html>


Session.IsCookieless

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            If Session.IsCookieless Then
               Message.Text = "The current Session does not use cookies."
            Else
               Message.Text = "The current Session uses cookies."
            End If
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>


Session.IsNewSession

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            If Session.IsNewSession Then
               "Session("foo") = "foo"
               Message.Text = "The current Session (SessionID: " & _
                  Session.SessionID & ") was created with this request."
            Else
               Message.Text = "The current Session (SessionID: " & _
                  Session.SessionID & ") existed prior to this request."
            End If
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>


Session.IsReadOnly

<%@ Page Language="vb" EnableSessionState="ReadOnly" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            If Session.IsReadOnly Then
               Message.Text = "The current Session (SessionID: " & _
                  Session.SessionID & ") is read-only for this page."
            Else
               Session("foo") = "foo"
               Message.Text = "The current Session (SessionID: " & _
                  Session.SessionID & ") can be written to from this page."
            End If
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>


Session.Item

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            Session.Item("foo") = "foo"
            Session.Item("foo2") = "foo2"
            Message.Text = Session.Item("foo") & "</br>"
            Message.Text &= Session.Item(1)
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>


Session.Keys

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            Dim Key As String
            Message.Text = "Session Keys:"
            For Each Key in Session.Keys
               Message.Text &= "</br>Key:" & Key
               Message.Text &= "</br>Value:" & Session(Key)
            Next
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>


Session.LCID

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            Message.Text = "Current locale ID is: " & Session.LCID & "</br>"
            Message.Text &= "Current date and time is: " & DateTime.Now() & "</br>"
            Session.LCID = 1036 "France
            Message.Text &= "Current locale ID is: " & Session.LCID & "</br>"
            Message.Text &= "Current date and time is: " & DateTime.Now() & "</br>"
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>


Session.Mode

<%@ Page Language="vb" %>
<html>
   <head>
      <title>Session property example</title>
      <script runat="server">
         Sub Page_Load()
            Message.Text = "Current Session State Mode: " & Session.Mode.ToString()
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>


Session.Remove

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            If Not Session("foo") Is Nothing Then
               Session.Remove("foo")
               Message.Text = "Item "foo" was removed."
            Else
               Message.Text = "Item "foo" does not exist."
            End If
         End Sub
      </script>
   </head>
<body>
   <form runat="server">
      <asp:label id="Message" runat="server"/>
   </form>
</body>
</html>


Session.RemoveAll

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            If Session.Count > 0 Then
               Session.RemoveAll()
               Message.Text = "Session collection cleared."
            Else
               Message.Text = "Session collection is already empty."
            End If
         End Sub
      </script>
   </head>
<body>
   <form runat="server">
      <asp:label id="Message" runat="server"/>
   </form>
</body>
</html>


Session.RemoveAt

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            If Session.Count > 0 Then
               Session.RemoveAt(0)
               Message.Text = "The item at index 0 was removed."
            Else
               Message.Text = "The item at index 0 does not exist."
            End If
         End Sub
      </script>
   </head>
<body>
   <form runat="server">
      <asp:label id="Message" runat="server"/>
   </form>
</body>
</html>


Session.StaticObjects

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            Message.Text = "There are " & Session.StaticObjects.Count & _
               " objects declared with the " & _
               "&lt;object runat=&quot;server&quot;&gt; syntax in Session scope."
            Dim myobj As Object
            For Each myObj in Session.StaticObjects
               If myObj.Value.GetType.ToString() = _
                  "System.Web.UI.WebControls.TextBox" Then
                  myForm.Controls.Add(myObj.Value)
               End If
            Next
         End Sub
      </script>
   </head>
<body>
   <form id="myForm" runat="server">
      <asp:label id="Message" runat="server"/>
   </form>
</body>
</html>


Session.Timeout

<%@ Page Language="vb" %>
<html>
   <head>
      <script runat="server">
         Sub Page_Load()
            Message.Text = "Current Session timeout value is " & _
               Session.Timeout & " minutes."
         End Sub
      </script>
   </head>
<body>
   <asp:label id="Message" runat="server"/>
</body>
</html>


Use Session and Server.Transfer to create a wizard

<%@Page language="c#" runat="server" Debug="true" %>
<script runat="server">
protected void Page_Load(object o, EventArgs e) {
    if(IsPostBack) {
        Session["theName"] = (string)theName.Text;
        Server.Transfer("Default.aspx", false);
    }
}
</script>
<form runat="server">
    Name: <asp:textbox id="theName" runat="server" />
    <asp:button type="submit" id="submitStep1" runat="server" Text="Go"/>
</form>
File: Default.aspx

<%@Page language="c#" runat="server" %>
<script runat="server">
protected void Page_Load(object o, EventArgs e) {
    if(IsPostBack) {
    Session["theHobby"] = theHobby.Text;
    Server.Transfer("NextPage.aspx");
    }
}
</script>
<form runat="server">
    Hobby: <asp:textbox id="theHobby" runat="server"/>
    <asp:button type="submit" id="submitStep2" runat="server" Text="Go"/>
</form>
File: NextPage.aspx
<%@Page language="c#" runat="server" %>
<script runat="server">
protected void Page_Load(object o, EventArgs e) {
    theFinalName.Text = (string)Session["theName"];
    theFinalHobby.Text = (string)Session["theHobby"];   
}
</script>
<form runat="server">
Done!<br />
    Name: <asp:label id="theFinalName" runat="server" /><br/>
    Hobby: <asp:label id="theFinalHobby" runat="server" /><br/>
</form>