在我的web表單中,我有2個只能接受數字的字段(它們的值的計算結果應該顯示在第三個字段中,這兩個字段的RegularValidation在用戶點擊提交按鈕之前工作正常,但不是如何處理它在webform中驗證
<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);
的可能的複製[?如何讓一個文本框僅整數(http://stackoverflow.com/questions/9732455/how-to-allow-only-integers-in-a -textbox) – techspider
不......它完全不同於此 – Biju