无法在 C 中获得正确的布尔值结果#



我有一个表单,我想在处理表单之前检查验证。我的表单有2个部分,所以我要确保每个部分至少有一个项目被选中时,他们按下提交按钮,如果他们这样做,然后去Dashboard.aspx。即使我在检查result1和result2时输入了所有必需的信息,也会得到false。Result1和Result2不会得到正确的值。即使值再次为True,它也传递false。

下面是我的代码:
    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值。

Try this

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

编辑

您期望的行为是out参数修饰符的行为。但是请不要这样写代码…

我编辑你的代码来摆脱…em . .繁琐的东西。

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

由于您正在传递Result1Result2作为参数,而不是分配它们。结果将永远不会被设置。

这是一种正确的方法

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

和一个边注。我认为我们可以安全地从CheckWireFromValidation方法中删除布尔参数。因为返回值不依赖于输入变量

最新更新