为什么条件块被执行?

时间:2016-01-15 11:50:22

标签: c# asp.net webforms

我有这个条件

if (txtBoxFatherHusbandName.Text != "" || txtBoxName.Text != "" || txtBoxNICNo.Text != "")
{
    ShowMsgBox("Please first <b>Save/Update</b> the data being entered in mandatory fields");    
    txtBoxFatherHusbandName.Focus();    
    return;
}

所有三个文本框都是空的,但没有文本,但仍然会执行条件。为什么?

3 个答案:

答案 0 :(得分:3)

你必须在这里使用String.IsNullOrWhiteSpace()来检查它们,因为在这种情况下用户可能会按空格可能会出现问题,所以你应该写下来,如果像下面那样处理空文本框:

if (!String.IsNullOrWhiteSpace(txtBoxFatherHusbandName.Text) || .......)

答案 1 :(得分:1)

在条件

中使用以下语句
if(!string.IsNullOrEmpty(txtBoxFatherHusbandName.Text.Trim()))

答案 2 :(得分:0)

if (!string.IsNullOrWhiteSpace(txtBoxFatherHusbandName.Text) || !string.IsNullOrWhiteSpace(txtBoxName.Text) || !string.IsNullOrWhiteSpace(txtBoxNICNo.Text))
    {
        ShowMsgBox("Please first <b>Save/Update</b> the data being entered in mandatory fields");

        txtBoxFatherHusbandName.Focus();

        return;
    }
相关问题