Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ASP.NET data validation control CustomValidator

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "ASP.NET data validation control CustomValidator how to use", the content is easy to understand, clear, hope to help you solve doubts, the following let Xiaobian lead you to study and learn "ASP.NET data validation control CustomValidator how to use" this article.

Overview of ASP.NET data validation Control CustomValidator Control

Simply using the first four data validation controls mentioned earlier, you can achieve the validation requirements of most of the data forms we generally develop. However, sometimes we need to validate some of the more complex forms. Imagine that we have a more complex questionnaire, which contains a form with many radio buttons and multiple buttons, some of which correspond to personal hobbies, such as swimming, sports, reading, and so on. The questionnaire wants respondents to choose all their hobbies. Under these options, the prompt is as follows: "if you are prompted to have two hobbies of sports and swimming, please choose when you will start learning to swim" and place some age-specific radio buttons below these text. Whether these buttons are effective depends on the selection of the multiple selection buttons we mentioned earlier. In the above validation case, we will use the CustomValidator data validation control.

In all ASP.NET data validation controls, there is a "ControlToValidate" control property that needs to be set to specify the form items in the form that need to be validated. In the CustomValidator data validation control, you need to do the following:

1. Read form item values that need to be validated by CustomValidator

2. Perform a verification operation

3. Determine whether the verified form item meets the verification requirements.

In other built-in controls, users do not know and ignore the above steps, but when using CustomValidator, we need to implement the above steps ourselves. In order to implement the above steps, we need to write a server-side data validation function in the following style:

Sub FunctionName (sender as Object, args as ServerValidateEventArgs)... End Sub

Notice the args parameter of the above function, which is the second parameter of the FunctionName function, which has the following two properties:

1. Value: the value of the validated data form.

2. IsValid: verify whether it is passed. If passed, the value of IsValid is True;. If it fails, the value is False.

When using CustomValidator, we not only have to set the "ControlToValidate" property, we must also set the OnServerValidate event to handle the server-side data validation action.

Create a simple ASP.NET data validation control CustomValidator

Suppose we now set up a math-related website in which we ask visitors to provide their favorite prime numbers in order to increase the number of visits to our website. In ASP.NET, there is no ready-made server-side data validation control that can specifically validate prime numbers, so it is necessary for us to use the CustomValidator control.

The following example is used to verify that the number entered by the user is prime. In this form, there is a TextBox control and a Button control for the user to enter data, a CustomValidator control for verifying whether it is a prime number, and two CompareValidator controls for ensuring that the user input is a positive 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 CustomValidator control--% > < asp:CustomValidator runat= "server" id= "custPrimeCheck "ControlToValidate=" txtPrimeNumber "OnServerValidate=" PrimeNumberCheck "ErrorMessage=" Invalid PrimeNumber "/ > <%-- create two CompareValidator controls: * guaranteed input as numbers The second guarantee is a positive number -% > < 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 >

If you are not too familiar with determining prime numbers, you may feel confused about the above PrimeNumberCheck event handling. First, the data entered by the user at the txtPrimeNumber is transmitted to the txtPrimeNumber through the args.value of the event, then the user's input is square root, and then the number entered by the user is divided by each number from 2 to the square root just calculated. If the result is zero, the number entered by the user is not a prime number. Set args.isValid to False;. If all zero, the user input is legal, and args.isValid is set to True.

Client-side Verification of ASP.NET data validation Control

One of the major features of ASP.NET built-in data validation controls is that all their validation is carried out on the client side and does not need to go through the server side, while the error verification of the CustomValidator control is completely realized through the server side. It is important to realize that data validation on the server side is bound to occur regardless of whether data validation is implemented on the client side or not. The addition of client-side validation only makes our data validation controls more friendly.

In order to implement client-side verification, we must write a script function using JavaScript or VBScript, because VBScript only supports IE browsers, so here we use JavaScript to write this function:

< script language= "JavaScript" >

Share To

Development

© 2024 shulou.com SLNews company. All rights reserved.

12
Report