代码之家  ›  专栏  ›  技术社区  ›  CodeMan

如果不满足else条件,但仍在内部运行代码

  •  1
  • CodeMan  · 技术社区  · 9 年前

    这是我遇到的一个非常不寻常的问题,我的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中的类型:

    enter image description here

    enter image description here

    如您所见,上述条件失败,但仍运行该代码:

    enter image description here

    enter image description here 如果您需要任何代码或逻辑理解,请在标记此问题之前询问。

    以下是我的情况:

    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());
        }
    }
    
    2 回复  |  直到 9 年前
        1
  •  3
  •   Dan Orlovsky    9 年前

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

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

    这基本上是说:

    如果监督员在此列表中,且TextBox102等于“安全”,则运行条件。或者,如果TextBox102等于“质量”,则运行条件。或者,如果TextBox102等于“常规”,则运行条件。

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

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

    请注意,文本框的每个复选框周围都有一组额外的括号。这将您的IF语句转换为:

    如果主管在列表中,且TextBox102=安全或TextBox1102=一般或TextBox202=质量,则运行条件。

        2
  •  1
  •   Chris Berger    9 年前

    (编辑:实际上,这里有点像忍者丹·奥洛夫斯基(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")