.net之session(例程)

上篇博客列举了session的一些基本知识,这篇来举一些例程


Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    输入session值:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="保存session值到session['text_session']"/>
    <div>
    
    </div>
    <p>
        输入一串数字:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" Text="拆分为数组,存入session['text_arr']" 
            οnclick="Button2_Click" />
    </p>
    <p>
        日历:</p>
    <asp:Calendar ID="Calendar1" runat="server" BackColor="#FFFFCC" 
        BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" 
        Font-Names="Verdana" Font-Size="8pt" ForeColor="#663399" Height="200px" 
        ShowGridLines="True" Width="220px">
        <DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
        <NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
        <OtherMonthDayStyle ForeColor="#CC9966" />
        <SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
        <SelectorStyle BackColor="#FFCC66" />
        <TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" 
            ForeColor="#FFFFCC" />
        <TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
    </asp:Calendar>
    <asp:Button ID="Button3" runat="server" Text="将calendar赋值给session['calendar']" 
        οnclick="Button3_Click" />
    <br />
    <br />
    覆盖session[ss_text]的值:<asp:Button ID="Button4" 
        runat="server" Text="覆盖" οnclick="Button4_Click" />
    <br />
    <br />
    </form>
</body>
</html>
Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["ss_text"] = (TextBox1.Text == "") ? "空字符串" : TextBox1.Text;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string[] a = TextBox2.Text.Split(',');
        Session["ss_arr"] = a;
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Session["ss_cal"] = Calendar1;
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Session.Add("ss_text", 123456);
    }
}


Default2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        读取ss_session的值:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        获取数组:</div>
    <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Height="84px"></asp:TextBox>
    <br />
    获取控件:<br />
    <asp:Panel ID="Panel1" runat="server">
    </asp:Panel>
    <p>
        遍历session:<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
    </p>
    <p>
        获取sessionid:<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
    </p>
    <p>
        获取session过期时间:<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
    </p>
    <p>
        删除所有session值:<asp:Button ID="Button1" runat="server" Text="删除" 
            οnclick="Button1_Click" />
    </p>
    <p>
        <asp:Button ID="Button2" runat="server" Text="移除session['ss_text']" 
            οnclick="Button2_Click" />
    </p>
    </form>
</body>
</html>


Default2.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = (Session["ss_text"] != null) ? Session["ss_text"].ToString() : "session值为空";


        TextBox1.Text = "";
        if (Session["ss_arr"] != null)
        {
            string[] a = (string[])Session["ss_arr"];

            for (int i = 0; i < a.Length; i++)
            {
                TextBox1.Text += a[i] + "\n";
            }
        }


        if (Session["ss_cal"] != null)
        {
            Calendar _cal = (Calendar)Session["ss_cal"];
            Panel1.Controls.Add(_cal);
        }


        Label2.Text = "";
        foreach (string _key in Session.Keys)
        {
            Label2.Text += "[" + _key + "]  " + Session[_key].GetType().ToString()+"\n";
        }

        Label3.Text = Session.SessionID.ToString();

        Label4.Text = Session.Timeout.ToString();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect(Request.Url.LocalPath.ToString());
    }


    protected void Button2_Click(object sender, EventArgs e)
    {
        Session.Remove("ss_text");
        Response.Redirect(Request.Url.LocalPath.ToString());
    }
}

运行结果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值