这是我在页面加载时运行的代码:
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();
}
}
有时,当我尝试将一些数据保存到数据库时出现错误时,我尝试通过单击“保存”按钮重新保存数据,但出现此错误:
你调用的对象是空的
在以下行(如果有代码):
else if (Session["AccessLevel"].ToString().Equals("2"))
为什么会出现此错误?
这是我在登录用户控件中的代码,其中ChR是记住用户的复选框:
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");
}
在我的母版页中,我在page_Load事件中以这种方式为会话分配值:
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
最佳答案
我想我找出了为什么我的会话变为空的原因!当我想将有关一本书的信息保存到db并将该书的图像文件保存在app文件夹中时,我无权保存该文件,并且我的应用程序抛出错误,并且我的所有会话都变为null!