ASP.NET中的主页上的会话变量为null

时间:2013-12-24 08:42:55

标签: c# asp.net-mvc session-variables

在我的登录页面代码中:

DB成功后

 protected void btnLogin_Click(object sender, EventArgs e)
    {
        int intErr = 0;
        LoginDetails objLogDetails = new LoginDetails();
        objLogDetails.UserName = txtUsername.Value.ToString();
        objLogDetails.UserPassword = txtUserpassword.Value.ToString();

    if (Request.Form["rdoLogType"] != null)
        sType = Request.Form["rdoLogType"].ToString();
    else if (Request.Form["rdoLogType"] == null && txtUsername.Value.ToString().ToLower() == "admin")
        sType = "3";
    else
        sType = "0";
    try
    {
        if (Request.Form["rdoLogType"] == null)
        {
            intErr = 1;
            divErrMsg.InnerText = "Please Select Login type.";
            divErrMsg.Visible = true;
        }
        else
        {
            int intLogId = 0;
            ServeAtDoorstepService objService = new ServeAtDoorstepService();
            if (sType == "1")
                intLogId = objService.LoginCustomer(objLogDetails);
            if (sType == "2")
                intLogId = objService.LoginVendor(objLogDetails);
            if (sType == "3")
                intLogId = objService.LoginCustomer(objLogDetails);

            if (string.IsNullOrEmpty(intLogId.ToString().Trim()))
            {
                intErr = 1;
                divErrMsg.InnerText = "Invalid Loginname and Password! Please try again.";
                divErrMsg.Visible = true;
            }
            else
            {
                Session.Abandon();
                Session.RemoveAll();

                Session.Add("LoginId", intLogId.ToString());
                Session.Add("LoginType", sType);

                if (chkAgree.Checked == true)
                {
                    HttpCookie cLoginId = new HttpCookie("LoginId", intLogId.ToString().Trim());
                    HttpCookie cLoginType = new HttpCookie("LoginType", sType);
                    cLoginId.Expires = DateTime.Now.AddDays(5);
                    cLoginType.Expires = DateTime.Now.AddDays(5);

                    Response.Cookies.Add(cLoginId);
                    Response.Cookies.Add(cLoginType);

                    HttpCookie cLoginName = new HttpCookie("LoginName", txtUsername.Value.ToString().Trim());
                    HttpCookie cPassword = new HttpCookie("Password", txtUserpassword.Value.ToString().Trim());
                    cLoginName.Expires = DateTime.Now.AddDays(5);
                    cPassword.Expires = DateTime.Now.AddDays(5);

                    Response.Cookies.Add(cLoginName);
                    Response.Cookies.Add(cPassword);
                }
            }
        }
    }
    catch (System.Threading.ThreadAbortException)
    {
    }
    catch (Exception ex)
    {
        divErrMsg.InnerText = ex.Message.ToString();
        intErr = 1;
    }

    if (intErr == 0 && Session["LoginType"].ToString() == "1")
        Response.Redirect("MyCustomerDash.aspx");
    else if (intErr == 0 && Session["LoginType"].ToString() == "2")
        Response.Redirect("MyVendorDash.aspx");
    else if (intErr == 0 && Session["LoginType"].ToString() == "3")
        Response.Redirect("MyAdminDash.aspx");
}

在我的母版页面上 这个母版页对所有页面都是通用的

 protected void Page_Load(object sender, EventArgs e)
    {
        isShowHideControl = "0";
        lblWelcomeMsg.Visible = false;
        lblDashboard.Visible = false;

        if (Session["LoginId"] != null && (Session["LoginType"] != null && Session["LoginType"].ToString() == "3"))
        {
            lblWelcomeMsg.Visible = true;
            lblDashboard.Visible = true;
            isShowHideControl = "3"; // Admin
        }
        else if (Session["LoginId"] != null && (Session["LoginType"] != null && Session["LoginType"].ToString() == "2"))
        {
            lblWelcomeMsg.Visible = true;
            lblDashboard.Visible = true;
            isShowHideControl = "2"; // Vendor
        }
        else if (Session["LoginId"] != null && (Session["LoginType"] != null && Session["LoginType"].ToString() == "1"))
        {
            lblWelcomeMsg.Visible = true;
            lblDashboard.Visible = true;
            isShowHideControl = "1"; // Customer
        }
    }

LoginIdLoginType始终是null

请帮助我获取会话价值。

2 个答案:

答案 0 :(得分:0)

这很可能是由于母版页/网页表单的事件生命周期。我不完全确定,但桅杆页Load事件可能会在您Click事件后触发。有关详细信息,请参阅此页:http://msdn.microsoft.com/en-us/library/dct97kc3.ASPX

答案 1 :(得分:0)

请参阅msdn Session.Abandon

  

当调用Abandon方法时,当前的Session对象排队等待删除,但在处理完当前页面上的所有脚本命令之前,实际上并未删除。这意味着您可以在与调用Abandon方法相同的页面上访问存储在Session对象中的变量,但不能访问任何后续Web页面。   例如,在以下脚本中,第三行打印值Mary。这是因为在服务器处理完脚本之前,Session对象不会被销毁。

<% 
  Session.Abandon  
  Session("MyName") = "Mary" 
  Reponse.Write(Session("MyName")) 
%> 

因此,在您的示例中,您在重定向之前放弃了会话,而在其他页面上它变得不可用。因此,对于解决方法,请尝试从登录页面中的代码中删除此行

Session.Abandon();