当前位置:编程学习 > C#/ASP.NET >>

求大神解释一下 Page_Load里的session啥作用,以及后边userId 是如何判断的,咋由-1变到>0 的

public partial class login_ : System.Web.UI.Page
{
    private static string constr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\demo.mdf;Integrated Security=True;User Instance=True";
    

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["IsLogin"] != null)
        {
            if ((bool)Session["IsLogin"] == true)
            {
                state.Text = "注销";
            }
            else
            {
                state.Text = "登陆";
            }
        }
        else
        {
            state.Text = "登陆";
        }
    }
    protected void login_click(object sender, EventArgs e)
    {
        string name = u_name.Text;
        string pass = u_pass.Text;
        int userId = -1;
        string comtxt = @"select id from useInfo where name='{0}'and pass='{1}';";

        if (name != "" && pass != "")
        {
            SqlConnection conn = new SqlConnection();
            SqlCommand comm = new SqlCommand();
            conn.ConnectionString = constr;
            comm.Connection = conn;
            comtxt = string.Format(comtxt, name, pass);
            comm.CommandText = comtxt;
           
            conn.Open();
            try
            {
                userId = (int)comm.ExecuteScalar();

            }
            catch 
            {
                userId = -1;
            }
            conn.Close();

            if (userId >= 1)
            {
                tip.Text = "登陆成功!";
                Session["IsLogin"] = true;
                Session["UserId"] = userId;
                state.Text = "注销";
            }
            else
            {
                tip.Text = "登陆失败,请检查用户名和密码!"; 
            }
        }
        else
        {
            tip.Text = "用户名和密码不能为空!";
        }

    }
    protected void state_out(object sender, EventArgs e)
    {
        if (Session["IsLogin"] != null)
        {
            if ((bool)Session["IsLogin"] != false)
            {
                Session["IsLogin"] = false;
                Session["UserId"] = -1;
                state.Text = "登陆";
            }
        }
    }
} --------------------编程问答-------------------- 登陆成功,将userid保存到session里:
if (userId >= 1)
  {
  tip.Text = "登陆成功!";
  Session["IsLogin"] = true;
  Session["UserId"] = userId;
  state.Text = "注销";
  }

在页面加载时,再从session内读取,判断用户状态 --------------------编程问答-------------------- Session可以看成是一个时间段内的全局变量,只要浏览器不关闭或变量未超时,变量就可以在本站内的所有动态脚本作出响应 --------------------编程问答-------------------- session 是利用服务器内存存储一个可设定存储时间的变量

一般默认session存在时间是20分钟

这个变量在存在时间内可以在网站的任何动态页面里获取使用

当然可以释放session变量或者覆盖原有的 --------------------编程问答-------------------- 这就是一个登录程序
session是判断里面有没有存入一个值,如果值不为空,并且为Ture的话就进下一步了。

结果开始赋值为-1,然后查询用户名和密码是否正确,正确的话就会返回1.错的话就返回0了。也就是进入登陆失败的条件了。

--------------------编程问答-------------------- 小手一抖,十分到手,拎着楼主,低头猛走
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,