在我的网络表单中,我有2个字段,只能接受数字(它们的值的计算结果应显示在第三个字段中。这两个字段的RegularValidation在用户点击提交按钮之前工作正常。但不是之后。如何处理它?</ p>
<asp:TextBox ID="TextBox1" runat="server" placeholder="Liter"></asp:TextBox><span style="color:red;font-weight:bold"> *</span>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="This is required" ForeColor="Red" ValidationGroup="test" ControlToValidate="TextBox1" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" ControlToValidate="TextBox1" runat="server" ForeColor="red" ErrorMessage="Enter only numbers!" ValidationExpression="\d+" Display ="Dynamic"></asp:RegularExpressionValidator>
提交按钮:
protected void btn_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
TextBox3.Text = ((Convert.ToInt32(TextBox1.Text) * 4.18 * Convert.ToInt32(TextBox2.Text)) / 3600).ToString();
double result = Convert.ToDouble(TextBox3.Text);
TextBox3.Text = String.Format("{0:0.00}", result);
答案 0 :(得分:0)
你可以编写一个这样的函数/方法来传递TextBox的值
public static bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
{
Int32 result;
return Int32.TryParse(val, NumberStyle,
System.Globalization.CultureInfo.CurrentCulture, out result);
}
调用并检查方法,例如传递以下内容
var _isNumeric2 = isNumeric("9.", System.Globalization.NumberStyles.Integer);
将第一个参数替换为TextBox3.Text值
答案 1 :(得分:0)
只需添加ValidationGroup="test"
字段即可。解决方案是:
<asp:TextBox ID="TextBox1" runat="server" placeholder="Liter"></asp:TextBox><span style="color:red;font-weight:bold"> *</span>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="This is required" ForeColor="Red" ValidationGroup="test" ControlToValidate="TextBox1" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" ControlToValidate="TextBox1" runat="server" ForeColor="red" ErrorMessage="Enter only numbers!" ValidationExpression="\d+" ValidationGroup="test" Display ="Dynamic"></asp:RegularExpressionValidator>