C# Can';在C中得不到正确的布尔结果#

C# Can';在C中得不到正确的布尔结果#,c#,validation,boolean-expression,C#,Validation,Boolean Expression,我有一个表格,我想在处理表格之前检查是否有效。我的表单有两个部分,所以我想确保在他们按下提交按钮时,每个部分中至少有一个项目被选中,如果他们选择了,则转到Dashboard.aspx。即使我在检查result1和result2时输入了所有必需的信息,我也会得到false。Result1和Result2将无法获得正确的值。即使这些值再次为真,也会传递为假 这是我的密码: protected void btnSumbit_Click(object sender, EventArgs e)

我有一个表格,我想在处理表格之前检查是否有效。我的表单有两个部分,所以我想确保在他们按下提交按钮时,每个部分中至少有一个项目被选中,如果他们选择了,则转到Dashboard.aspx。即使我在检查result1和result2时输入了所有必需的信息,我也会得到false。Result1和Result2将无法获得正确的值。即使这些值再次为真,也会传递为假

这是我的密码:

    protected void btnSumbit_Click(object sender, EventArgs e)
    {
        if (!Page.IsValid)
            return;

        bool result1 = false;
        bool result2 = false;
        CheckWireFromValidation(result1);
        CheckWireToValidation(result2);
        if (result1 == true && result2 == true)
        {
            Response.Redirect("~/DashBoard.aspx");
        }
    }

    public bool CheckWireFromValidation (bool result1)
    {
        if (drpFromCoporate.SelectedIndex != 0 || drpFromCapital.SelectedIndex != 0 || drpFromProperty.SelectedIndex != 0)
        {
            result1 = true;
        }
        else
        {
            result1 = false;
            ShowAlertMessage("You need to choose at least one filed from Wire From drop downs!!");
        }         
        return result1;
    }


    public bool CheckWireToValidation(bool result2)
    {
        if (drpToCapital.SelectedIndex != 0 || drpToCoporate.SelectedIndex != 0 || drpToProperty.SelectedIndex != 0 || drpToTemplate.SelectedIndex != 0 || txtCorpAmt.Text != "" || txtCapAmt.Text != "" || txtPropAmt.Text != "" || txtTempelateAmt.Text != "")
        {
            result2 = true;
        }
        else
        {
            ShowAlertMessage("You need to choose at least one filed from Wire To drop downs!!");
        }
        return result2;      
    }

您没有使用
CheckWireToValidation
的结果。您正在使用最初分配的
false

试试这个

    bool result1 = false;
    bool result2 = false;

    if (CheckWireFromValidation(result1) && CheckWireToValidation(result2))
    {
        Response.Redirect("~/DashBoard.aspx");
    }
编辑 你所期待的行为就是你的孩子。但是请不要这样写代码

我编辑了你的代码以摆脱。。相对长度单位。。克鲁夫特。这应该更具可读性

protected void btnSumbit_Click(object sender, EventArgs e)
{
    if (!Page.IsValid)
        return;

    if (CheckWireFromValidation() && CheckWireToValidation())
    {
        Response.Redirect("~/DashBoard.aspx");
    }
}

public bool CheckWireFromValidation ()
{
    if (drpFromCoporate.SelectedIndex != 0 || drpFromCapital.SelectedIndex != 0 || drpFromProperty.SelectedIndex != 0)
    {
        return true;
    }
    else
    {          
        ShowAlertMessage("You need to choose at least one filed from Wire From drop downs!!");
        return false;
    }         
}

public bool CheckWireToValidation ()
{
    if (drpToCapital.SelectedIndex != 0 || drpToCoporate.SelectedIndex != 0 || drpToProperty.SelectedIndex != 0 || drpToTemplate.SelectedIndex != 0 || txtCorpAmt.Text != "" || txtCapAmt.Text != "" || txtPropAmt.Text != "" || txtTempelateAmt.Text != "")
    {
        return true;
    }
    else
    {
        ShowAlertMessage("You need to choose at least one filed from Wire To drop downs!!");
        return false;
    }     
}

您没有使用
CheckWireToValidation
的结果。您正在使用最初分配的
false

试试这个

    bool result1 = false;
    bool result2 = false;

    if (CheckWireFromValidation(result1) && CheckWireToValidation(result2))
    {
        Response.Redirect("~/DashBoard.aspx");
    }
编辑 你所期待的行为就是你的孩子。但是请不要这样写代码

我编辑了你的代码以摆脱。。相对长度单位。。克鲁夫特。这应该更具可读性

protected void btnSumbit_Click(object sender, EventArgs e)
{
    if (!Page.IsValid)
        return;

    if (CheckWireFromValidation() && CheckWireToValidation())
    {
        Response.Redirect("~/DashBoard.aspx");
    }
}

public bool CheckWireFromValidation ()
{
    if (drpFromCoporate.SelectedIndex != 0 || drpFromCapital.SelectedIndex != 0 || drpFromProperty.SelectedIndex != 0)
    {
        return true;
    }
    else
    {          
        ShowAlertMessage("You need to choose at least one filed from Wire From drop downs!!");
        return false;
    }         
}

public bool CheckWireToValidation ()
{
    if (drpToCapital.SelectedIndex != 0 || drpToCoporate.SelectedIndex != 0 || drpToProperty.SelectedIndex != 0 || drpToTemplate.SelectedIndex != 0 || txtCorpAmt.Text != "" || txtCapAmt.Text != "" || txtPropAmt.Text != "" || txtTempelateAmt.Text != "")
    {
        return true;
    }
    else
    {
        ShowAlertMessage("You need to choose at least one filed from Wire To drop downs!!");
        return false;
    }     
}

因为您将
Result1
Result2
作为参数传入,而不是分配它们。结果永远不会确定

这里有一个正确的方法

bool result1 = CheckWireFromValidation(result1);
bool result2 = CheckWireToValidation(result2);

if (result1 == true && result2 == true)
{
    Response.Redirect("~/DashBoard.aspx");
}
还有一个旁注。我认为我们可以安全地从
CheckWireFromValidation
方法中删除布尔参数。因为返回值不依赖于输入变量


希望这能有所帮助。

因为您将
Result1
Result2
作为参数传入,而不是分配它们。结果永远不会确定

这里有一个正确的方法

bool result1 = CheckWireFromValidation(result1);
bool result2 = CheckWireToValidation(result2);

if (result1 == true && result2 == true)
{
    Response.Redirect("~/DashBoard.aspx");
}
还有一个旁注。我认为我们可以安全地从
CheckWireFromValidation
方法中删除布尔参数。因为返回值不依赖于输入变量


希望这有帮助。

非常感谢您的回答。我没注意到,谢谢你的回答。我没有注意到