Asp.net| control asp.net 1.1, there are many validation controls, greatly facilitate us, but sometimes, when it is necessary to do special validation, it will feel inadequate, so we can use the custom validation control CustomValidator, to use this control, The corresponding event must be written on the service side, in the following format:
Sub functionname (sender as Object, args as ServerValidateEventArgs)
...
End Sub
Note the two parameter value: Indicates which control the current CustomValidator verifies IsValid: When True, the page control has passed validation. An example of verifying whether the input of a text box in a page is a prime number: <script language= "vb" runat= "Server" >
Sub btnSubmit_Click (sender as Object, E as EventArgs)
If Page.IsValid Then
Response.Write ("<font color=" "Red" "><i>" & Txtprimenumber.text & _
"Is, indeed, a good prime number.</i></font>")
Else
Response.Write ("<font color=" "Red" "><i>" & Txtprimenumber.text & _
"Is <b>not</b> a prime number.</i></font>")
End If
End Sub
Sub Primenumbercheck (sender as Object, args as ServerValidateEventArgs)
Dim iprime as Integer = Cint (args. Value), Iloop as Integer, _
Isqrt as Integer = CInt (math.sqrt (iprime))
For iloop = 2 to Isqrt
If iprime MoD iloop = 0 Then
Args. IsValid = False
Exit Sub
End If
Next
Args. IsValid = True
End Sub
</script>
<form method= "POST" runat= "Server" >
Enter your favorite prime number:
<asp:textbox id= "Txtprimenumber" runat= "Server"/>
<%--Create the CustomValidator control--%>
<asp:customvalidator runat= "Server" id= "Custprimecheck"
Controltovalidate= "Txtprimenumber"
Onservervalidate= "Primenumbercheck"
Errormessage= "Invalid Prime number"/>
<%--Create two CompareValidator controls:the
The number entered by the "user is" an Integer; The second
Makes sure it is positive. --%>
<asp:comparevalidator runat= "Server" id= "Compprimenumber"
Operator= "DataTypeCheck" type= "Integer"
Display= "Dynamic" controltovalidate= "Txtprimenumber"
ErrorMessage = "You must enter an integer value."/>
<asp:comparevalidator runat= "Server" id= "Compprimenumberpositive"
Operator= "GreaterThan" type= "Integer"
Display= "Dynamic" valuetocompare= "0"
Controltovalidate= "Txtprimenumber"
ErrorMessage = "You must enter a value greater than zero."/>
<p><asp:button id= "btnsubmit" runat= "Server"
onclick= "btnSubmit_Click" text= "Submit"/>
</form>
As you can see, in the validation control, in Onservervalidate= "Primenumbercheck", you define a specific OnServerValidate event
, then, in the specific event processing, it is important to return the value of Args.isvalid, to explain whether the validation is successful; and, of course, the Page.IsValid attribute for full validation
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service