A problem that has been ignored for a long time: the onclientclick event of the control and button is verified.

Source: Internet
Author: User

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:

Code
  < Form ID = "Form1" Runat = "Server" >
< ASP: scriptmanager ID = "Scriptmanager1" Runat = "Server" >
</ ASP: scriptmanager >
< Div >
< ASP: textbox ID = "Textboxtest" Runat = "Server" > </ ASP: textbox >
< ASP: requiredfieldvalidator ID = "Requiredfieldvalidator1" Runat = "Server" Controltovalidate = "Textboxtest"
Errormessage = "Cannot be blank" Display = "NONE" > </ ASP: requiredfieldvalidator > < Ajaxtoolkit: validatorcalloutextender
ID = "Validatorcalloutextender1" Targetcontrolid = "Requiredfieldvalidator1" Runat = "Server" >
</ Ajaxtoolkit: validatorcalloutextender >
< ASP: button ID = "Buttontext" Runat = "Server" Text = "Test" Onclientclick = "Return confirm ('Are you sure you want to submit? ');"   />
</ Div >
</ Form >

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 checkedSource codeThe following source code is generated by the buttontest control:Code:

<Input type = "Submit" name = "buttontext" value = "test" onclick = "javascript: webform_dopostbackwithoptions (New webform_postbackoptions (& quot; buttontext & quot;, & quot; & quot;, true, & quot;, & quot;, false, false) "id =" buttontext "/>

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:

<Input type = "Submit" name = "buttontext" value = "test" onclick = "Return confirm ('Are you sure you want to submit? '); Webform_dopostbackwithoptions (New webform_postbackoptions (& quot; buttontext & quot;, & quot;, true, & quot;, & quot ;, false, false) "id =" buttontext "/>

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. My solution is to execute custom JavaScript (Return confirm ('Are you sure you want to submit? '), It is necessary to verify whether the control in the page meets the rules, so I modified the onclientclick event of buttontest as follows:

Code
 <ASP: buttonID= "Buttontext"Runat= "Server"Text= "Test"Onclientclick= "If (checkclientvalidate () return confirm ('Are you sure you want to submit the page? ');" />

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

Code
< Script Language = " Javascript " Type = " Text/JavaScript " >
Function Checkclientvalidate (){
Page_clientvalidate ();
If (Page_isvalid ){
Return   True ;
} Else {
Return   False ;
}
}
< / SCRIPT>

Run and test. Verify the role of the control. Solve the problem.

Iv. Postscript

This is the problem and solution 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 terrible. It can also be seen from here that it is necessary to specify strict server-side verification :-). It not only prevents hackers from bypassing client verification, but also prevents data inaccuracy due to errors that you have not detected.

This is my solution, not whether the garden friends have other solutions. Please contact us.

 

 

 

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.