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:
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:
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.