c# - if else 条件不满足但仍然运行里面的代码

标签 c# asp.net if-statement web-config

这是我遇到的一个非常不寻常的问题,我的 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 并运行应用程序它仍然运行第三个条件。假设说您没有权限。

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());
    }
}

最佳答案

因此,在评论中发现一些新信息后,我们可以解决 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.

关于c# - if else 条件不满足但仍然运行里面的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41005309/

相关文章:

c# - EWS 托管 API : Setup user email alias

javascript - 使用 jquery 启用/禁用对 radio 选择的控制

c# - 如何在gridview中实现排序功能?

python - Python 中包含多个字符串的 if 语句

c# - 这还是闭包吗?

c# - 将两个正则表达式组合成一个键/值对对象?

c# - NEST Elasticsearch查询值在2个字段名之间

asp.net - 为 CAC 卡配置 'Cassini'(ASP.NET 开发服务器)以进行测试 - 如何?

powershell foreach-object 与 if 语句

php - 我如何在这种情况下使用 if/then/else?