在设置验证时,我最初遇到了一些问题:如何检查文本框是否为空。我尝试使用
private void btnGo_Click(object sender, EventArgs e)
{
string name = textLogin.Text;
if (name == null)
{
labelError.Visiblle = true;
labelError.Text = "Field Cannot be Left Blank"
}
}
但是它没有起作用,直到我尝试了这个。
private void btnGo_Click(object sender, EventArgs e)
{
string name = textLogin.Text;
if (name == "")
{
labelError.Visiblle = true;
labelError.Text = "Field Cannot be Left Blank"
}
}
我的问题是我想了解("")和(null)之间的区别以及为什么null无法工作。
提前感谢。
string.IsNullOrEmpty()
是您的好朋友,因为在验证时希望将它们视为相同的情况非常普遍。 - millimoose