ASP.NET Session 使用方法

 
ASP.NET Session 使用方法
 

   Default.aspx
 
   <form id="form1" runat="server">
      <div>
         <asp:Label ID="Label1" runat="server" Text="Label" />  
         <asp:Button ID="Button1" runat="server" Text="Create Session" onclick="Create_Click" />&nbsp;&nbsp;
         <asp:Button ID="Button2" runat="server" Text="Destroy Session" onclick="Destroy_Click" />&nbsp;&nbsp;
         <asp:Button ID="Button3" runat="server" Text="Get Session" onclick="Get_Click" />&nbsp;&nbsp;
      </div>
   </form>
 

 

   Default.aspx.cs
 
   using System;
   using System.Web;
 
   namespace WebApplication3
   {
      public partial class _Default : System.Web.UI.Page
      {
         static int i = 0;
 
         protected void Page_Load(object sender, EventArgs e)
         {
            System.Web.SessionState.HttpSessionState ss = HttpContext.Current.Session;
 
            if (ss != null && ss["test"] != null)
            {
               this.Label1.Text = ss["test"].ToString();
            }
            else
            {
               this.Label1.Text = "Start Point";
            }
         }
 
         protected void Create_Click(object sender, EventArgs e)
         {
            System.Web.SessionState.HttpSessionState ss = HttpContext.Current.Session;
            i = 0;
            if (ss != null)
            {
               ss["test"] = i.ToString();
               this.Label1.Text = ss["test"].ToString();
            }
         }
 
         protected void Get_Click(object sender, EventArgs e)
         {
            System.Web.SessionState.HttpSessionState ss = HttpContext.Current.Session;
            if (ss != null)
            {
               if (ss["test"] != null && ss["test"].ToString() != "")
               {
                  i++;
                  ss["test"] = i.ToString();
                  this.Label1.Text = ss["test"].ToString();
               }
               else
               {
                  this.Label1.Text = "Null Value";
               }
            }
         }
 
         protected void Destroy_Click(object sender, EventArgs e)
         {
            System.Web.SessionState.HttpSessionState ss = HttpContext.Current.Session;
            if (ss != null)
            {
               ss.RemoveAll();
               if (ss != null && ss["test"] != null && ss["test"].ToString() != "")
               {
                  i++;
                  this.Label1.Text = ss["test"].ToString();
               }
               else
               {
                  this.Label1.Text = "Null Value";
               }
            }
         }
      }
   }
 

 

   WebForm1.aspx
 
   <form id="form1" runat="server">
      <div>
         <asp:Label ID="Label1" runat="server" Text="Label" />
      </div>
   </form>
 

 

   WebForm1.aspx.cs
 
   using System;
   using System.Web;
 
   namespace WebApplication3
   {
      public partial class WebForm1 : System.Web.UI.Page
      {
         protected void Page_Load(object sender, EventArgs e)
         {
            System.Web.SessionState.HttpSessionState ss = HttpContext.Current.Session;
            if (ss != null && ss["test"] != null && ss["test"] != "")
            {
               this.Label1.Text = ss["test"].ToString();
            }
            else
            {
               this.Label1.Text = "Null Value";
            }
         }
      }
   }
 

 

   WebForm2.aspx
 
   <form id="form1" runat="server">
      <div>
         <asp:Label ID="Label1" runat="server" Text="Label" />
      </div>
   </form>
 

 

   WebForm2.aspx.cs
 
   using System;
   using System.Web;
 
   namespace WebApplication3
   {
      public partial class WebForm2 : System.Web.UI.Page
      {
         protected void Page_Load(object sender, EventArgs e)
         {
            System.Web.SessionState.HttpSessionState ss = HttpContext.Current.Session;
            if (ss != null && ss["test"] != null && ss["test"] != "")
            {
               this.Label1.Text = ss["test"].ToString();
            }
            else
            {
               this.Label1.Text = "Null Value";
            }
         }
      }
   }