这是我遇到的一个非常不寻常的问题,如果条件不起作用,我的其他问题: 1)我正在从web.config中读取值
string validuserlist = ConfigurationManager.AppSettings["Quality"].ToString();
string safetylist = ConfigurationManager.AppSettings["Safety"].ToString();
string supervisorlist = ConfigurationManager.AppSettings["Supervisors"].ToString();
2)我正在检查当前用户是否在上面的列表中,以及工作订单类型是否来自gridview:
在检查supervisorlist的第三个条件下没有满足条件我从web.config中删除了我的用户ID并且运行应用程序它仍然运行第三个条件。假设你没有权限。
正如您所看到的,上述条件失败但仍然运行该代码:
如果您需要任何代码或逻辑理解,请提供帮助,请在标记此问题之前询问。
这是我的病情如何布局:
if (validuserlist.ToLower().Trim().IndexOf(username.ToLower().Trim()) != -1 && TextBox102.Text == "Quality")
{
CheckQuality();
if (flag == true)
{
ModalPopupExtender1.Show();
TextBox TextBox1 = (TextBox)DetailsView1.FindControl("TextBox30");
TextBox TextBox2 = (TextBox)DetailsView1.FindControl("TextBox91");
TextBox1.Enabled = true;
TextBox2.Enabled = true;
DetailsView1.Visible = true;
ModalPopupExtender2.Show();
DetailsView2.Visible = true;
}
else
{
string message = "your user id does not have permissions to signoff WorkOrders of type" + " " + TextBox102.Text + ", please contact IT Support for Permission";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}
}
else if (safetylist.ToLower().Trim().IndexOf(username.ToLower().Trim()) != -1 && TextBox102.Text == "Safety")
{
CheckSafety();
if (flag == true)
{
ModalPopupExtender1.Show();
TextBox TextBox1 = (TextBox)DetailsView1.FindControl("TextBox30");
TextBox TextBox2 = (TextBox)DetailsView1.FindControl("TextBox91");
TextBox1.Enabled = true;
TextBox2.Enabled = true;
DetailsView1.Visible = true;
ModalPopupExtender2.Show();
DetailsView2.Visible = true;
}
else
{
string message = "your user id does not have permissions to signoff WorkOrders of type" + " " + TextBox102.Text + ", please contact IT Support for Permission";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}
}
else if (supervisorlist.ToLower().Trim().IndexOf(username.ToLower().Trim()) != -1 && TextBox102.Text == "Safety" || TextBox102.Text == "Quality" || TextBox102.Text == "General")
{
if (flag == false)
{
ModalPopupExtender1.Show();
TextBox TextBox1 = (TextBox)DetailsView1.FindControl("TextBox30");
TextBox TextBox2 = (TextBox)DetailsView1.FindControl("TextBox91");
TextBox1.Enabled = true;
TextBox2.Enabled = true;
DetailsView1.Visible = true;
ModalPopupExtender2.Show();
DetailsView2.Visible = true;
}
else
{
string message = "your user id does not have permissions to signoff WorkOrders of type" + " " + TextBox102.Text + ", please contact IT Support for Permission";
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script type = 'text/javascript'>");
sb.Append("window.onload=function(){");
sb.Append("alert('");
sb.Append(message);
sb.Append("')};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
}
}
答案 0 :(得分:3)
因此,在评论中发现一些新信息之后,我们可以解决if条件的错误。
else if (supervisorlist.ToLower().Trim().IndexOf(username.ToLower().Trim()) != -1 && TextBox102.Text == "Safety" || TextBox102.Text == "Quality" || TextBox102.Text == "General")
这基本上是在说:
如果主管在此列表中并且TextBox102等于&#34;安全&#34;运行条件。或者,如果TextBox102等于&#34;质量&#34;,则运行条件。或者如果TextBox102等于&#34; General&#34;,则运行条件。
看来您想知道主管是否在该列表中,并且如果文本框等于其中一个,那么@Aidin建议,您的IF语句应如下所示:
else if (supervisorlist.ToLower().Trim().IndexOf(username.ToLower().Trim()) != -1 && (TextBox102.Text == "Safety" || TextBox102.Text == "Quality" || TextBox102.Text == "General"))
注意TextBox的每个检查周围的额外括号集。这会将您的IF语句转换为:
如果主管在列表中并且TextBox102 = Safety OR TextBox102 = General或TextBox102 = Quality,请运行条件。
答案 1 :(得分:1)
(编辑:实际上,有点忍者Dan Orlovsky在这里,因为他在我发布的同时在评论中找到答案。他的答案更加详细。)
supervisorlist.ToLower().Trim().IndexOf(username.ToLower().Trim()) != -1
&& TextBox102.Text == "Safety"
|| TextBox102.Text == "Quality"
|| TextBox102.Text == "General"
看起来像操作顺序问题。在您的条件周围加上括号。像这样:
supervisorlist.ToLower().Trim().IndexOf(username.ToLower().Trim()) != -1
&& (TextBox102.Text == "Safety"
|| TextBox102.Text == "Quality"
|| TextBox102.Text == "General")