Verify the OnClientClick event parsing of controls and buttons _ javascript skills

Source: Internet
Author: User
The following are the problems and solutions that I have known to be ignored. When I found this problem, I experienced a cold sweat. Fortunately, I did a strict server verification. Otherwise, it would be miserable. I. Events

This is a problem that has been ignored for a long time or has not been found. The problem is as follows:

On a page, when a verification control exists, when the Button control triggers the OnClientClick event and the event returns true and false, the verification control becomes invalid and does not take effect. The detailed description is as follows:

The. Net page is as follows:

The Code is as follows:




As shown above, add the RequireFieldValidator verification control to the page so that the TextBoxTest value cannot be blank. When submitting the page in ButtonText, you need to confirm whether to submit it. There seems to be no problem with a simple page. However, when the TextBoxTest value is empty, the verification control does not work and the page is submitted successfully. Why?

2. Event Response

What's going on? First, after removing the OnClientClick event of ButtonTest, verify that the control works. Why? I checked the source code of the page and found that the ButtonTest control generated the following source code:

From this line of source code, we can see that the verification control generates a piece of javascript code on the client and verifies whether the value in TextBox is null. After I added the OnClientClick of ButtonTest, I checked the source code again. The source code generated by the ButtonTest control is as follows:

From this line of code, we can clearly see where the problem is. on the client side, we first execute custom javascript, and then execute the javascript generated by the verification control, obviously, in this case, the verification control loses any meaning.

3. Response Control

Once you know where the problem is, it's easy to handle it. My solution is: Execute the custom javascript (return confirm ('Are you sure you want to submit? '), It is necessary to verify whether the controls on the page comply with the rules, so I modified the OnClientClick event of ButtonTest as follows:

The Code is as follows:




The code for the CheckClientValidate () method is as follows:

The Code is as follows:


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.