ASP.NET会话当地的回传后成为空 [英] ASP.NET Session becomes null after postback on local

查看:231
本文介绍了ASP.NET会话当地的回传后成为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码是在页面加载运行:

 保护无效的Page_Load(对象发件人,EventArgs五)
{
DisableChaching();
如果(Request.Cookies时[用户名] == NULL)
{
如果(会话[用户名] == NULL)
{
响应。重定向(〜/ Default.aspx的);
}
,否则如果(会话[ACCESSLEVEL]。的ToString()。等于(2))
{
的Response.Redirect(〜/ Default.aspx的 );
}
}
,否则如果(会话[ACCESSLEVEL]。的ToString()。等于(2))
{
的Response.Redirect(〜 /Default.aspx);
}
如果
{
LoadControls()(的IsPostBack!);
BindGrid();
}
}



有时,当我尝试保存一些数据到数据库,我得到一个错误,我尝试的再保存的点击我的保存按钮,我得到这个错误的数据:




对象引用未设置为一个对象




在以下行的一个实例,如果代码:

 否则如果(会话[ACCESSLEVEL]。的ToString()。等于(2))

为什么会出现这个错误?



下面是我在登录用户的控制代码,其中CHR是的复选框记住用户:

 如果(ChR.Checked ==真)
{
。Response.Cookies [用户名]值= txtUserName.Text.Trim();
Response.Cookies [用户名] =到期DateTime.Now.AddMonths(2);
Response.Cookies [ACCESSLEVEL]值= member.AccessLevel.ToString();
Response.Cookies [ACCESSLEVEL] =到期DateTime.Now.AddMonths(2);
Response.Cookies [名字]值= member.FirstName。
Response.Cookies [名字] =到期DateTime.Now.AddMonths(2);
Response.Cookies [姓氏]值= member.LastName。
Response.Cookies [姓氏] =到期DateTime.Now.AddMonths(2);
会话[用户名] = txtUserName.Text.Trim();
会话[ACCESSLEVEL] = member.AccessLevel.ToString();
的Response.Redirect(〜/ Default.aspx的);
}
,否则
{
会话[用户名] = txtUserName.Text.Trim();
会话[ACCESSLEVEL] = member.AccessLevel.ToString();
会话[名字] = member.FirstName;
会话[姓氏] = member.LastName;
的Response.Redirect(〜/ Default.aspx的);
}

和在我的母版页我值分配给会话这种方式在Page_Load事件

  DisableChaching(); 
FillInfo();
如果(Request.Cookies时[用户名]!= NULL)
{
会话[用户名] = Request.Cookies时[用户名]值。
会话[ACCESSLEVEL] = Request.Cookies时[ACCESSLEVEL]值。
会话[名字] = Request.Cookies时[名字]值。
会话[姓氏] = Request.Cookies时[姓氏]值。
惠康();
如果(会话[ACCESSLEVEL]的ToString()==1)。
{
RenderMenu(AcccessLevel.SiteManager);
}
,否则如果
{
RenderMenu(AcccessLevel.Client)(会话[ACCESSLEVEL]的ToString()==2);
}
}
,否则如果(会话[用户名]!= NULL)
{
惠康();
如果(会话[ACCESSLEVEL]的ToString()==1)。
{
RenderMenu(AcccessLevel.SiteManager);
}
,否则如果
{
RenderMenu(AcccessLevel.Client)(会话[ACCESSLEVEL]的ToString()==2);
}

}
,否则
{
威固();
RenderMenu(AcccessLevel.LogedOutUser);这里
}
输入代码


解决方案

我想我找到了,为什么我会成为空!当我要救一本书的信息在DB和保存图书的图像文件中的app文件夹,我没有保存文件的权限和我的应用程序抛出一个错误,我的所有会话变为空!


Here is my code that runs on page load:

protected void Page_Load(object sender, EventArgs e)
{
    DisableChaching();
    if (Request.Cookies["UserName"] == null)
    {
        if (Session["UserName"] == null)
        {
            Response.Redirect("~/Default.aspx");
        }
        else if (Session["AccessLevel"].ToString().Equals("2"))
        {
            Response.Redirect("~/Default.aspx");
        }
    }
    else if (Session["AccessLevel"].ToString().Equals("2"))
    {
        Response.Redirect("~/Default.aspx");
    }
    if (!IsPostBack)
    {
        LoadControls();
        BindGrid();
    }
}

Sometimes when I try to save some data to database and I get an error, I try to re-save the data by clicking my save button, and I get this error:

Object reference not set to an instance of an object

on the following line if code:

else if (Session["AccessLevel"].ToString().Equals("2"))

Why am I getting this error?

here is my code in Login user control where ChR is the checkbox to remember the user :

if (ChR.Checked == true)
            {
                Response.Cookies["UserName"].Value = txtUserName.Text.Trim();
                Response.Cookies["UserName"].Expires = DateTime.Now.AddMonths(2);
                Response.Cookies["AccessLevel"].Value = member.AccessLevel.ToString();
                Response.Cookies["AccessLevel"].Expires = DateTime.Now.AddMonths(2);
                Response.Cookies["FirstName"].Value = member.FirstName;
                Response.Cookies["FirstName"].Expires = DateTime.Now.AddMonths(2);
                Response.Cookies["LastName"].Value = member.LastName;
                Response.Cookies["LastName"].Expires = DateTime.Now.AddMonths(2);
                Session["UserName"] = txtUserName.Text.Trim();
                Session["AccessLevel"] = member.AccessLevel.ToString();
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                Session["UserName"] = txtUserName.Text.Trim();
                Session["AccessLevel"] = member.AccessLevel.ToString();
                Session["FirstName"] = member.FirstName;
                Session["LastName"] = member.LastName;
                Response.Redirect("~/Default.aspx");
            }

and in my master page I assign values to sessions this way in the page_Load event:

DisableChaching();
    FillInfo();
    if (Request.Cookies["UserName"] != null)
    {
        Session["UserName"] = Request.Cookies["UserName"].Value;
        Session["AccessLevel"] = Request.Cookies["AccessLevel"].Value;
        Session["FirstName"] = Request.Cookies["FirstName"].Value;
        Session["LastName"] = Request.Cookies["LastName"].Value;
        WellCome();
        if (Session["AccessLevel"].ToString() == "1")
        {
            RenderMenu(AcccessLevel.SiteManager);
        }
        else if (Session["AccessLevel"].ToString() == "2")
        {
            RenderMenu(AcccessLevel.Client);
        }
    }
    else if (Session["UserName"] != null)
    {
        WellCome();
        if (Session["AccessLevel"].ToString() == "1")
        {
            RenderMenu(AcccessLevel.SiteManager);
        }
        else if (Session["AccessLevel"].ToString() == "2")
        {
            RenderMenu(AcccessLevel.Client);
        }

    }
    else
    {
        WellGo();
        RenderMenu(AcccessLevel.LogedOutUser);
    }
enter code here

解决方案

I think I found out why my sessions become null ! when I want to save info about a book in db and save an image file of the book in an app folder , I don't have the permission to save the file and my application throws an error and all my sessions become null !!!

这篇关于ASP.NET会话当地的回传后成为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆