• 【学习笔记】ASP.NET页面之间传值的方式之Session


      Session

    Session在ASP.NET中,表示客户端(Goggle,Firefox,IE等)与服务器端的会话,用来存储特定会话信息,准确来说,是用来存储特定用户信息。当客户端向服务器发送一个请求时,如登陆用户ID,服务器接收到该请求,服务器端Session产生一个与该登陆用户相关的SessionID,并将SessioID返回给客户端(Goggle,Firefox,IE等),在新会话开始时,服务器将SessionID当做cookie存储在用户的浏览器中。

    Session操作与Application类似,作用于用户个人,所以,过量的存储会导致服务器内存资源的耗尽。

    为什么引入Session?大家知道,因为http是一种无状态协议,因此,Session正弥补了这一缺陷。当然,Session作用远远不止于这些,这里就不多论述。

      优点:1.使用简单,不仅能传递简单数据类型,还能传递对象。
         2.数据量大小是不限制的。

      缺点:1.在Session变量存储大量的数据会消耗较多的服务器资源。

         2.容易丢失。(重启IIS服务器时Session丢失)

      ps:可以配置把Session数据存储到SQL Server数据库中,为了进行这样的配置,程序员首先需要准备SQL Server数据服务器,然后在运行.NET自带安装工具安装状态数据库。 这种方式在服务器挂掉重启后都还在,因为他存储在内存和磁盘中。

      使用方法:1.在源页面的代码中创建你需要传递的名称和值构造Session变量:Session["Name"]="Value(Or Object)";

           2.在目的页面的代码使用Session变量取出传递的值。Result = Session["Nmae"]

      注意:session不用时可以销毁它,销毁的方法是:清除一个:Session.Remove("session名");  清除所有:Session.Clear();

      (1)a.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="a.aspx.cs" Inherits="WebApplication.a" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="Label1" runat="server" Text="张君宝"></asp:Label>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
            </div>
        </form>
    </body>
    </html>

      (2)a.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication
    {
        public partial class a : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                Session["name"] = Label1.Text;//把label1的值放进Session
                Session.Timeout = 1;//设置Session的作用时间是一分钟,一分钟后Session失效
                Response.Redirect("b.aspx");
            }
        }
    }

      (3)b.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="b.aspx.cs" Inherits="WebApplication.b" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </div>
        </form>
    </body>
    </html>

      (4)b.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication
    {
        public partial class b : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Label1.Text = Session["name"].ToString();
            }
        }
    }

      (5)运行a页面,点击Button后进入b页面,一分钟之后刷新页面会报错“未将对象实例化”,正常,那是因为Session已经失效!

                             

    ps:此文章是本人参考网上内容加上自己的理解整合而成,如无意中侵犯了您的权益,请与本人联系。

  • 相关阅读:
    【DevExpress v17.2新功能预告】DevExtreme TreeList
    MyEclipse WebSphere开发教程:安装和更新WebSphere 6.1, JAX-WS, EJB 3.0(六)
    【DevExpress v17.2新功能预告】改进DevExtreme编辑器
    MyEclipse WebSphere开发教程:安装和更新WebSphere 6.1, JAX-WS, EJB 3.0(五)
    DevExpress DateEdit 5 常见问题解决方法
    CSS五种方式实现 Footer 置底
    css中雪碧图(sprite)的使用及制作方法
    CSS实现垂直居中的5种方法
    如何使用CSS创建巧妙的动画提示框
    css定位方式有哪几种?
  • 原文地址:https://www.cnblogs.com/kudsu/p/7702615.html
Copyright © 2020-2023  润新知