错误消息不适用于文本框值 [英] Error message not working for textbox value
本文介绍了错误消息不适用于文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网页表单,上面有三个文本框。前两个做计算,答案进入textbox3。我试图显示一条消息,只有当textbox3中的值为50或更大时才会显示该消息。这是我的代码:
if (TextBoxHC50.Text < 50 )
{
lblMessage.Text = 你的答案增加了50%以上,;
}
else if (TextBoxHC50.Text > 50 )
{
}
我做错了什么不能正常工作?
解决方案
首先尝试转换你的价值。
int iTextBoxValue = Convert.ToInt32(TextBoxHC50.Text);
if (iTextBoxValue < 50 ){
lblMessage.Text = 你的答案增加了50%以上,;
} else if (iTextBoxValue > 50 ){
}
你无法比较一个整数值的字符串,就像你的50一样。(你不应该硬代码立即常量;这对于维护是不好的;更好地声明显式常量,只做一次。)
另外,您没有考虑到文本框输入可能不是表示整数值的有效字符串。假设您要使用int
类型,请使用int.TryParse
。您也可以使用int.Parse
,但随后准备好处理可能的异常。
-SA
I have a web form that has three textboxes on it. The first two do calculation and the answer goes into textbox3. I am trying to display a message that will only display if the value in textbox3 is 50 or greater. Here is my code:
if (TextBoxHC50.Text < 50)
{
lblMessage.Text = "Your answer has increased more than 50%,";
}
else if (TextBoxHC50.Text > 50)
{
}
What am I doing wrong for this not to work?
解决方案
Try converting your value first.
int iTextBoxValue = Convert.ToInt32(TextBoxHC50.Text); if (iTextBoxValue < 50) { lblMessage.Text = "Your answer has increased more than 50%,"; } else if (iTextBoxValue > 50) { }
You cannot compare a string with integer value, like your 50. (You should not also hard-code immediate constants; this is bad for maintenance; better declare explicit constants, do it just once.)
Also, you are not taking into account that the text box input can be not a valid string representing an integer value. Assuming you want to work with theint
type, useint.TryParse
. You could useint.Parse
, too, but then get ready to handle possible exceptions.
—SA
这篇关于错误消息不适用于文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文