这是我在页面加载时运行的代码:
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