ASP.NET页面传值(二)--Session页面传值

Session

想必这个肯定是大家使用中最常见的用法了,其操作与Application类似,作用于用户个人,所以,过量的存储会导致服务器内存资源的耗尽。

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

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

2.容易丢失。

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

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

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

                         清除所有:Session.Clear();

 例子:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="one.aspx.cs" Inherits="AST.NET.one" %>

<!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:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="传值" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

    第一个页面

注意添加using指令(using System.Data;)

protected void Button1_Click(objectsender, EventArgs e)

        {
            //---Session传值
            //服务端,大量传值增大服务端压力

 
           Session["name"] = TextBox1.Text;

           Response.Redirect("two.aspx");

        }

第二个页面

只需在前端添加lable即可,注意添加using指令(using System.Data;)

protected void Page_Load(objectsender, EventArgs e)

        {

            Label1.Text =Session["name"].ToString();

        }

效果:



猜你喜欢

转载自blog.csdn.net/hemingyang97/article/details/81021993