会话变量比较不起作用 [英] session variable comparison not working
本文介绍了会话变量比较不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
if (Session["Type"].ToString().Equals("Admin", StringComparison.CurrentCultureIgnoreCase))
{
LinkButton_logout.Text = "Logout";
Label_loggedin.Text = "Welcome " + Session["Alias"].ToString();
}
else if (Session["Type"] == null)
{
Response.Redirect("Default.aspx");
}
链接按钮文字和标签文字不变......甚至不重定向默认页面
link button text and label text not changing ...not even redirecting to default page
推荐答案
如果Session [Type]是null
,那么尝试调用ToString它将在第一个if
条件下抛出Null Reference异常 - 这将终止操作。
更改测试的顺序 - 总是首先检查null
,永远不会持续!
If Session["Type"] isnull
, then the attempt to call ToString on it will throw a "Null Reference" exception on the firstif
condition - which will terminate the operation.
Change the order of your tests - always check fornull
first, never last!
首先你要检查null然后去比较。代码应该看起来像
First you should check against null then go for comparison. The code should look like
string type = Session["Type"] as string;
if (null == type)
{
Response.Redirect("Default.aspx", true);
}
if (type.Equals("Admin",StringComparison.OrdinalIgnoreCase))
{
LinkButton_logout.Text = "Logout";
Label_loggedin.Text = "Welcome " + Convert.ToString(Session["Alias"]);
}
试试这个
Try this
if (Session["Type"] == null)
{
Response.Redirect("Default.aspx");
}
else if(String.Equals(Convert.ToString(Session["Type"]), "Admin",StringComparison.OrdinalIgnoreCase))
{
LinkButton_logout.Text = "Logout";
Label_loggedin.Text = "Welcome " + Convert.ToString(Session["Alias"]);
}
注意:在此代码之前,请检查您的会话是否具有要处理的正确值。
问候.. :)
Note: Before this code,check that your sessions have proper values to be handled.
Regards..:)
这篇关于会话变量比较不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文