if else条件不满足但还是运行里面的代码

if else condition not met but still running the code inside

这是我遇到的一个非常不寻常的问题,我的 if else if 条件不起作用: 1) 我正在读取 web.config

中的值
string validuserlist = ConfigurationManager.AppSettings["Quality"].ToString();
string safetylist = ConfigurationManager.AppSettings["Safety"].ToString();
string supervisorlist = ConfigurationManager.AppSettings["Supervisors"].ToString();

2) 我正在检查当前用户是否在上面的列表中,以及工单类型是否 = 来自 gridview 的类型:

在检查主管列表的第三个条件下不满足条件我从 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());
    }
}

(编辑:实际上,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")

因此,在评论中发现一些新信息后,我们可以解决 if 条件的问题。

else if (supervisorlist.ToLower().Trim().IndexOf(username.ToLower().Trim()) != -1 && TextBox102.Text == "Safety" || TextBox102.Text == "Quality" || TextBox102.Text == "General")

这基本上是在说:

If the supervisor is in this list AND TextBox102 is equal to "Safety" run the condition. OR if TextBox102 is equal to "Quality", run the condition. OR if TextBox102 is equal to "General", run the condition.

您似乎想知道主管是否在该列表中以及文本框是否等于其中之一,因此正如@Aidin 建议的那样,您的 IF 语句应如下所示:

else if (supervisorlist.ToLower().Trim().IndexOf(username.ToLower().Trim()) != -1 && (TextBox102.Text == "Safety" || TextBox102.Text == "Quality" || TextBox102.Text == "General"))

请注意每次检查 TextBox 时都多了一组括号。这会将您的 IF 语句变为:

If the supervisor is in the List AND TextBox102 = Safety OR TextBox102 = General OR TextBox102 = Quality, run the condition.