ASP.NET Tutorial/Sessions/Abandon

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

End a session by calling Session.Abandon (C#)

<%@ Page Language="C#" %>
<script runat="server">
   void Submit_Click(Object Sender, EventArgs e) {
      if (tbPass.Value == "mypass") {
         Session["UserName"] = tbUser.Value;
         Response.Redirect("NextPage.aspx");
      } else {
         Label1.Text = "<font color=red>That " + "is the wrong password!</font>";
      }
   }
</script>
<html>
<body>
   <form runat="server">
      Please enter your username and password:
      <input type="text" id="tbUser" runat="server"/><br>
      <input type="password" id="tbPass" runat="server"/>
      
      
      <asp:Button id="btSubmit" text="Submit" runat="server" OnClick="Submit_Click" />
      <asp:Label id="Label1" runat="server"/>
   </form>
</body>
</html>

File: NextPage.aspx
<%@ Page Language="VB" %>
<script runat="server">
   sub Page_Load(Sender as Object, e as EventArgs)
      if not Page.IsPostBack then
         Label1.Text = "Welcome " & Session("Username") & _
            "!"
         
         Label1.Text = Label1.Text & "The current time is: " & _
            DateTime.Now.ToString("T") & ""
      
         Label1.Text = Label1.Text & "Your session id is: " & _
            Session.SessionID & ""
      end if
   end sub
   
   sub Submit_Click(obj as object, e as eventargs)
      Session.Abandon
      Label1.Text = "Your session has ended."
      btSubmit.Visible = false
   end sub
</script>
<html><body>
   <form runat="server">
      <asp:Label id="Label1" runat="server"/>
      
      <asp:Button id="btSubmit" 
         text="Submit"
         runat="server"
         OnClick="Submit_Click" />
   </form>
</body></html>