我正在向某些用户显示Li
菜单,但会话超时后会收到错误:
未将对象引用设置为实例。
如果会话超时,如何避免此错误。
if (Session["LoggedInUser"].ToString() == "admin" || Session["LoggedInUserLower"].ToString() == "admin")
{
liBatch.Visible = true;
}
else
{
liBatch.Visible = false;
}
答案 0 :(得分:2)
在调用ToString
之前检查会话对象是否为空
if((Session["LoggedInUser"] != null && Session["LoggedInUser"].ToString()== "admin") ||
(Session["LoggedInUserLower"] != null && Session["LoggedInUserLower"].ToString() == "admin"))
{
liBatch.Visible = true;
}
else
{
liBatch.Visible = false;
}