protected void btnAddAppliance(object sender, EventArgs e)
{
var description = "";
var succeeded = true;
try
{
description = Convert.ToString(iptDescription);
if (string.IsNullOrEmpty(description) || string.IsNullOrWhiteSpace(description))
{
errormessage.Text = "You need to add a description!";
errormessage.Visible = true;
succeeded = false;
}
}
catch
{
errormessage.Text = "Enter a correct description!";
errormessage.Visible = true;
succeeded = false;
}
if (succeeded)
{
_c.NewAppliance(description);
grvAppliances.DataSource = _c.GetAllAppliances();
grvAppliances.DataBind();
iptDescription.Value = "";
}
}你好,我的代码有问题。这段代码使用IsNullOrEmpty()和IsNullOrWhiteSpace()方法检查我输入的值值是否不为空或不包含任何空格。每次我将输入留空时,它都会返回"System.Web.UI.HtmlControls.HtmlInputText",并将其放入我的MySQL数据库中,而不是提示我输入的错误消息。我该如何解决这个问题呢?
发布于 2020-05-11 04:44:13
您需要按照注释中的建议使用html控件元素的Value属性。因此,您应该使用Convert.ToString(iptDescription.Value)而不是Convert.ToString(iptDescription)
此外,除非被覆盖,否则ToString()将返回该类型的完全限定名。在本例中,HTML控件元素HtmlInputText
https://stackoverflow.com/questions/61718331
复制相似问题