ASP. NET-how to: Verify ASP. NET Server controls against database values

Source: Internet
Author: User

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;

}

}

}

 

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.