Requiredfieldvalidator in Asp.net is a tangle of issues (related to clientscript attributes)

Source: Internet
Author: User

First, let's take a look at the interface.

Interface requirements:

1. Click "create section" to save the input text in the text box to the database;

2. Click "edit" in the gridview to enter the record information in the text box;

3. The Service Code corresponds to the table field sectioncode that cannot be empty, and the ID is "tbsectionid ";

4. Add a requiredfieldvalidator, controltovalidate = "tbsectionid ";

Problems starting to tangle

First, set the enableclientscript of requiredfieldvalidator to true so that requiredfieldvalidator can be verified by the client.

Click the button to trigger the authentication and prompt that the sectionid cannot be blank to meet the requirements. However, clicking "edit" in the gridview will also trigger verification, so that the record information cannot be filled with the control.

Cause Analysis: enableclientscript = true, requiredfieldvalidator is used for client verification. Click "edit" to trigger client verification. In this case, the sectionid value is empty and verification fails, therefore, it cannot be uploaded back to the server, and therefore the text box cannot be filled with content.

Then, set the enableclientscript of requiredfieldvalidator to false to cancel client verification.

Click the button to prompt that the verification fails, but the content can be saved to the database. Click Edit. It also prompts that the verification fails, but the text box is filled successfully.

Cause of analysis: If requiredfieldvalidator's enableclientscript is set to false, the verification is performed on the server side. The verification is performed based on the sectionid value. If the verification fails, the isvalid of the page is set to false and the error message is displayed, however, the code on the server is executed later.

Solution: Add code to determine the verification result. The Event Response Function of the button is written in the following form:

Protected void onbutton_clicked (Object sender, eventargs E)

{

If (page. isvalid)

{

// Button event processing logic

....

}

}

(Note: personal opinions. If any error occurs, please do not hesitate to advise)

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.