点击计算按钮后,我无法验证使用输入。任何帮助将不胜感激。必须使用验证控件验证所有文本字段,并且应包含显示错误的摘要。我希望错误消息出现在我的lblMessage.Text中,但是一旦点击了Calculate就没有显示任何内容。 (侧面注意:我也无法通过“清除”按钮将文本框重置为NULL。帮助也很好)
HTML
<body>
<form id="form1" runat="server">
<div>
<img src="images/header.jpg" alt="Get Fit" title="GetFit" width="100%"/>
</div>
<h1>Body Mass Index Calculator</h1>
<table align="center">
<tr>
<td class="auto-style4">Enter your first name:</td>
<td class="auto-style6">
<asp:TextBox ID="txtName" runat="server" TabIndex="1"></asp:TextBox>
</td>
<td class="auto-style5"> </td>
<td class="auto-style7"> </td>
</tr>
<tr>
<td class="auto-style4">Enter your age:
</td>
<td class="auto-style6">
<asp:TextBox ID="txtAge" runat="server" TabIndex="2"></asp:TextBox>
</td>
<td class="auto-style5">
</td>
<td class="auto-style7">
<asp:CompareValidator ID="cv" runat="server" ControlToValidate="txtAge" Type="Integer" Operator="DataTypeCheck" ErrorMessage="Enter a valid age" >*</asp:CompareValidator>
</td>
</tr>
<tr>
<td class="auto-style4">Enter your email:
</td>
<td class="auto-style6">
<asp:TextBox ID="txtEmail" runat="server" TabIndex="3"></asp:TextBox>
</td>
<td class="auto-style5">
</td>
<td class="auto-style7">
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ControlToValidate="txtEmail" Display="Dynamic" ErrorMessage="Enter a valid email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="auto-style4">Enter your height:
</td>
<td class="auto-style6">
<asp:TextBox ID="txtHeight" runat="server" TabIndex="4"></asp:TextBox>
</td>
<td class="auto-style5">
<asp:Label ID="lblHeight" runat="server" Text="INCHES"></asp:Label>
</td>
<td class="auto-style7">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtHeight" Display="Dynamic" ErrorMessage="You must enter a valid height">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="auto-style4">Enter your weight:</td>
<td class="auto-style6">
<asp:TextBox ID="txtWeight" runat="server" TabIndex="5"></asp:TextBox>
</td>
<td class="auto-style5">
<asp:Label ID="lblWeight" runat="server" Text="POUNDS"></asp:Label>
</td>
<td class="auto-style7">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtWeight" Display="Dynamic" ErrorMessage="You must enter a valid weight">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="4">
<asp:Label ID="lblMessage" runat="server" Text="Label"></asp:Label>
</td>
</tr>
</table>
<br />
<table align="center">
<tr>
<td class="auto-style2">
<asp:Button ID="btnCalculate" runat="server" Text="Calculate BMI" align="center" TabIndex="6"/>
<span style="color:darkblue;">|</span>
<asp:Button ID="btnCancel" runat="server" Text="Cancel" align="center" TabIndex="7"/>
</td>
</tr>
</table>
<br />
<table align="center">
<tr>
<td>
<h1>Your BMI:</h1>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtBMI" runat="server" ReadOnly="True" TabIndex="8"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<h2>This means you are:
</h2>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="txtResults" runat="server" ReadOnly="True" TabIndex="9"></asp:TextBox>
</td>
</tr>
</table>
</form>
</body>
ASPX.VB
Partial Class _Default
Inherits System.Web.UI.Page
Public Sub ClearAll()
txtName.Text = ""
txtAge.Text = ""
txtEmail.Text = ""
txtHeight.Text = ""
txtWeight.Text = ""
End Sub
Protected Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
ClearAll()
'txtName.Text = String.Empty
'txtAge.Text = String.Empty
'txtEmail.Text = String.Empty
End Sub
Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
Calculate()
End Sub
Public Sub Calculate()
'Name
If txtName.Text = String.Empty Then
lblMessage.Text = "Please enter a name."
Else
End If
'Age
If txtAge.Text = String.Empty Then
lblMessage.Text = "Please enter an age."
Else
End If
'Email
If txtEmail.Text = String.Empty Then
lblMessage.Text = "Please enter an email."
Else
End If
'Height
If txtHeight.Text = String.Empty Then
lblMessage.Text = "Please enter a height."
Else
End If
'Weight
If txtWeight.Text = String.Empty Then
lblMessage.Text = "Please enter a height."
Else
End If
Dim Result As Double ' Declare "Result" as a Double.
Dim Rounded_Result As Double
Result = txtWeight.Text / (txtHeight.Text * txtHeight.Text) * 703 ' BMI Calculation.
Rounded_Result = Math.Round(Result, 1) ' Round result to 1 decimal place.
txtBMI.Text = Rounded_Result.ToString ' Display BMI using lblBMI.
BMI_Message(Rounded_Result) 'Send Rounded BMI result to Sub BMI_Message.
End Sub
Public Sub BMI_Message(ByVal BMI_Value As Double)
Select Case BMI_Value
Case 0.0 To 18.5
txtResults.Text = "Underweight"
Case 18.6 To 24.9
txtResults.Text = "Normal Weight"
Case 25.0 To 29.9
txtResults.Text = "Overweight"
Case Is >= 30.0
txtResults.Text = "Obese"
Case Else
txtResults.Text = "Unable to Determin BMI"
End Select
End Sub
End Class