我有一张包含约30个文本框的表格。
每行有2个文本框,这些文本框具有相同的前缀但后缀不同,例如txtAvg
From
和txtAvg
To
。
我需要遍历表并在文本框上执行自定义验证:
稍后我可能需要更多验证。此外,如果填写了to文本框,则不需要from,反之亦然。不需要文本框。
我需要进行服务器端验证。
我应该添加自定义验证控件吗?如果是,那么对于每个文本框?
我设置controltovalidate
了吗?
单击提交按钮时,我需要在每行旁边显示正确的错误。显然,如果页面无效,则不要再做任何事情了。
我不想添加不同的验证控件。只有自定义验证器。 我还需要动态执行此操作,当用户单击提交按钮时,我希望进行验证,并在错误行旁边显示(*)并在验证摘要中显示完整的错误消息。
我尝试添加控件但单击按钮时不会触发。
Private Sub AddCustomValidation(ByVal container As Control)
Dim custom As CustomValidator
Dim vldtxt As TextBox
For Each ctrl In container.Controls
If TypeOf ctrl Is TextBox Then
vldtxt = CType(ctrl, TextBox)
custom = New CustomValidator
custom.Text = "To field must be bigger than from field"
custom.ControlToValidate = vldtxt.ID
AddHandler custom.ServerValidate, AddressOf CustomValidation
' custom.ServerValidate += New System.Web.UI.WebControls.ServerValidateEventHandler(customvalidation)
Me.form1.Controls.Add(custom)
Else
AddCustomValidation(ctrl)
End If
Next
End Sub
Protected Sub CustomValidation(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
Dim vldtxt As TextBox
Dim othertxt As TextBox
Dim othertxtid As String
Dim txtid As String
Dim lower As String
Dim higher As String
For Each ctrl As Control In tbl1.Controls
vldtxt = TryCast(ctrl, TextBox)
othertxt = TryCast(ctrl, TextBox)
If vldtxt IsNot Nothing Then
txtid = ctrl.ID
''check if to is not empty and if not empty then check that that to value is higher
''than from value
If ((txtid.ToLower.EndsWith("to")) And (vldtxt.Text <> "")) Then
higher = (vldtxt.Text)
othertxtid = vldtxt.ID.Replace("To", "From")
othertxt = Page.FindControl(othertxtid)
othertxt.ID = othertxtid
lower = (othertxt.Text)
If lower = String.Empty Or higher = String.Empty Then
args.IsValid = True
End If
If Int32.Parse(lower) >= Int32.Parse(higher) Then
args.IsValid = False
End If
Else
args.IsValid = True
End If
End If
Next
End Sub
布局是这样的:
<table id="tbl1" runat="server" cellpadding="0" cellspacing="3">
<tr>
<td>From price</td>
<td>
<asp:TextBox runat="server" ID="txtPriceFrom">
</td>
<td>To price</td>
<td>
<asp:TextBox runat="server" ID="txtPriceTo">
</td>
<td>From yield</td>
<td>
<asp:TextBox runat="server" ID="txtYieldFrom">
</td>
<td>To yield</td>
<td>
<asp:TextBox runat="server" ID="txtYieldTo">
</td>
</tr>
</table>
答案 0 :(得分:1)
您可以使用ASP.NET验证控件:
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="if both to and from textboxes are filled in I have to check that the value in from textbox is smaller than the from value" >
</asp:CustomValidator>
<table>
<tr>
<td>From price</td>
<td>
<asp:TextBox runat="server" ID="txtPriceFrom"></asp:TextBox>
<asp:RegularExpressionValidator runat="server"
ControlToValidate="txtPriceFrom"
ErrorMessage="Please Enter Only Numbers"
ForeColor="Red"
ValidationExpression="^\d+$">
</asp:RegularExpressionValidator>
<asp:RangeValidator runat="server"
ControlToValidate="txtPriceFrom"
ErrorMessage="Range entered in each textbox is 1-100"
MaximumValue="100" MinimumValue="1">
</asp:RangeValidator>
</td>
<td>To price</td>
<td>
<asp:TextBox runat="server" ID="txtPriceTo"></asp:TextBox>
...
</td>
<td>From yield</td>
<td>
<asp:TextBox runat="server" ID="txtYieldFrom"></asp:TextBox>
...
</td>
<td>To yield</td>
<td>
<asp:TextBox runat="server" ID="txtYieldTo"></asp:TextBox>
...
</td>
</tr>
</table>