Home > Web Front-end > JS Tutorial > Detailed analysis of the OnClientClick event of the validation control and Button_javascript skills

Detailed analysis of the OnClientClick event of the validation control and Button_javascript skills

WBOY
Release: 2016-05-16 17:11:00
Original
1020 people have browsed it

1. Events

This is a problem that I have ignored or not discovered for a long time. The problem is as follows:

In a page, when there is a validation control, when the Button control triggers the OnClientClick event, and this event returns true and false, the validation control will become invalid and will no longer work. The specific description is as follows:

.Net page is as follows:

Copy code The code is as follows:






ErrorMessage="Cannot be null" Display="None">ID="ValidatorCalloutExtender1" TargetControlID="RequiredFieldValidator1" runat="server">





As above, add the RequireFieldValidator validation control to the page so that the value of TextBoxTest cannot be empty. When ButtonText submits the page, it must The user confirms whether submission is required. It's a very simple page and seems to have no problems. But when the value of TextBoxTest is empty, the validation control does not work and the page is submitted successfully. What is the reason for this?

2. Response to events

What’s going on? First, after I removed the OnClientClick event of ButtonTest, the verification control worked. Why is this? I checked the source code of the page and found that the ButtonTest control generated the following source code:

As can be seen from this line of source code, the validation control generates a piece of javascript code on the client side to verify whether the value in the TextBox is empty. After I added ButtonTest's OnClientClick, I re-checked the source code. The source code generated by the ButtonTest control is as follows:

From this line of code, you can clearly see where the problem lies. On the client side, the custom javascript is first executed, and then the javascript generated by the validation control is executed. Obviously, here In this case, the validation control loses any meaning.

3. Response controls

It will be easier to solve if you know where the problem is. My solution is: before executing the custom javascript (return confirm('Are you sure you want to submit?'), verify the controls on the page Does it comply with the rules, so I modified the OnClientClick event of ButtonTest as follows:

Copy the code The code is as follows:



The code of the CheckClientValidate() method is as follows:
Copy the code The code is as follows:



Run, test. Validation controls work. Problem solved.

4. Postscript

This is a problem and solution that I have known to ignore. When I discovered this problem, I broke out in a cold sweat. Fortunately, I did strict server-side verification, otherwise it would be miserable. From here we can also see how necessary it is to specify strict server-side verification :-). It not only prevents "hackers" from bypassing client verification, but also prevents data inaccuracies caused by errors that are not noticed by oneself.

Note:

Page_ClientValidate(), this function is used in aspx pages containing Microsoft validation controls to return True or False according to whether the user input operation is legal

Can be judged directly.

Copy code The code is as follows:

if(Page_ClientValidate())
{
return true;
}
else
{
return false;
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template