Session 传值

Session

保存在服务器上
很安全
占用服务器资源
只有会话模式   20分钟生命周期
可以存任意数据类型,Object,取的时候需要类型转换
服务器内存

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

public partial class Default1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Button1.Click += Button1_Click;
    }

    void Button1_Click(object sender, EventArgs e)
    {
        string uname = TextBox1.Text;//获取用户输入的用户名   
        string pwd = TextBox2.Text;//获取用户输入的密码
        Users u = new UsersData().SelectUser(uname, pwd);
        Session["hehe"] = u;//传值
        Response.Redirect("Default5.aspx");//跳到Default5.aspx
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["hehe"] != null)//不为空的时候执行
            Label1.Text = (Session["hehe"] as Users).NickName;//接受传过来的值  Session["hehe"] as Users变成对象
    }
}
原文地址:https://www.cnblogs.com/skyhorseyk/p/7299544.html