演示Session对象使用 范例(示例代码下载)

(一).说明

         用一个测试示例说明Session的使用方法, 包括以下方面:

              存储对象到Session中,设置监督Session的失效时间. 释放Session中存储变量.

(二).演示图例

        

 (三).代码

 <%@ Page language="c#"  AutoEventWireup="false"  %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Web"%>
<script language="cs" runat="server">
    private void btnAbandon_Click(object sender, System.EventArgs e)
 {
  Session.Abandon();
 }

 private void btnVar_Click(object sender, System.EventArgs e)
 {
  Session["Name"]=txtVar.Text;
  Session.Timeout=int.Parse(txtTime.Text);
 }

 private void btnShow_Click(object sender, System.EventArgs e)
 {
  name.Text=(string)Session["Name"];
  time.Text=Session.Timeout.ToString();
 }
</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>WebForm1</title>
  <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body bgColor="lightgoldenrodyellow" MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <asp:label id="Label1" style="Z-INDEX: 100; LEFT: 205px; POSITION: absolute; TOP: 134px" runat="server" ForeColor="LightSeaGreen" Font-Bold="True">Session变量:</asp:label><asp:label id="time" style="Z-INDEX: 113; LEFT: 382px; POSITION: absolute; TOP: 207px" runat="server"></asp:label><asp:button id="btnShow" OnClick="btnShow_Click" style="Z-INDEX: 107; LEFT: 340px; POSITION: absolute; TOP: 242px" runat="server" Text="显示会话变量"></asp:button><asp:button id="btnVar" OnClick="btnVar_Click" style="Z-INDEX: 106; LEFT: 178px; POSITION: absolute; TOP: 241px" runat="server" Text="会话变量设置"></asp:button><asp:label id="Label2" style="Z-INDEX: 102; LEFT: 204px; POSITION: absolute; TOP: 169px" runat="server" ForeColor="LightSeaGreen" Font-Bold="True">Session时间:</asp:label><asp:textbox id="txtVar" style="Z-INDEX: 101; LEFT: 338px; POSITION: absolute; TOP: 131px" runat="server"></asp:textbox>
    <asp:TextBox id="txtTime" style="Z-INDEX: 103; LEFT: 337px; POSITION: absolute; TOP: 165px" runat="server"></asp:TextBox>
    <asp:Button id="btnAbandon" OnClick="btnAbandon_Click" style="Z-INDEX: 104; LEFT: 501px; POSITION: absolute; TOP: 240px" runat="server" Text="终止会话"></asp:Button>
    <asp:Panel id="Panel1" style="Z-INDEX: 105; LEFT: 201px; POSITION: absolute; TOP: 43px" runat="server" Width="344px" Height="38px" ForeColor="#0000C0" Font-Size="X-Large">Session对象的使用范例</asp:Panel></FONT>
   <asp:Label id="name" style="Z-INDEX: 109; LEFT: 261px; POSITION: absolute; TOP: 205px" runat="server"></asp:Label>
   <asp:RequiredFieldValidator id="RequiredFieldValidator1" style="Z-INDEX: 110; LEFT: 504px; POSITION: absolute; TOP: 132px" runat="server" ErrorMessage="请输入Session变量名" ControlToValidate="txtVar">**</asp:RequiredFieldValidator>
   <asp:RequiredFieldValidator id="RequiredFieldValidator2" style="Z-INDEX: 111; LEFT: 506px; POSITION: absolute; TOP: 167px" runat="server" ErrorMessage="请输入Session时间" ControlToValidate="txtTime">**</asp:RequiredFieldValidator>
   <asp:ValidationSummary id="ValidationSummary1" style="Z-INDEX: 112; LEFT: 301px; POSITION: absolute; TOP: 298px" runat="server"></asp:ValidationSummary></form>
 </body>
</HTML>

(四).示例下载

        http://www.cnitblog.com/Files/ChengKing/SessionSample.rar    

 

 

 

 

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页