You can verify user input against the database to ensure that user input values are identifiable. To do this, you must write code in the customvalidator control to find data matching items in the database.
1. Add the customvalidator control to the page and set the following attributes:
Attribute |
Description |
Controltovalidate |
The ID of the control being verified. |
Errormessage, text, display |
These attributes specify the text and location of errors to be displayed when verification fails. |
2. Create an event handler for the servervalidate event of the customvalidator control. In the event handler, add code to search for the database and check user input against the dataset.
Note: If you leave the control blank, the control passes comparative verification. To force the user to enter a value, add the requiredfieldvalidator control.
3. Add a test in ASP. NET webpage code to check the validity.
The following code example demonstrates how to verify user input by querying in a database table. In this example, the email address stored in the table of comparison is verified by the email address entered by the user. The custom validation logic passes through the rows in the Table in sequence, which is part of the dataset that can be used on the page.
Private void customvalidator1_servervalidate (Object source, system. Web. UI. webcontrols. servervalidateeventargs ARGs)
{
Dataview DV;
Dataset dataset11 = new dataset ();
DV = dataset11.tables [0]. defaultview;
String txtemail;
Args. isvalid = false; // assume false
// Loop through table and compare each record against user's entry
Foreach (datarowview datarow in DV)
{
// Extract e-mail address from the current row
Txtemail = datarow ["alias"]. tostring ();
// Compare e-mail address against user's entry
If (txtemail = args. value)
{
Args. isvalid = true;
}
}
}