I. Events
This is a problem that I have long neglected or not found, and the problem is this:
In a page, when there is a validation control, when the button control triggers the OnClientClick event, and the event returns true and False, the validation control is invalidated and does not work. The detailed description is as follows:
. NET page is as follows:
Copy Code code as follows:
<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 empty" display= "None" ></asp:requiredfieldvalidator><ajaxtoolkit: Validatorcalloutextender
Id= "ValidatorCalloutExtender1" targetcontrolid= "RequiredFieldValidator1" runat= "Server" >
</ajaxToolkit:ValidatorCalloutExtender>
<asp:button id= "ButtonText" runat= "text=" test "onclientclick=" return confirm (' Are you sure you want to submit it? '); "/>
</div>
</form>
As above, add the Requirefieldvalidator validation control to the page so that the Textboxtest value cannot be empty, and when ButtonText submits the page, the user is asked to confirm whether the submission is required. A very simple page, seemingly there is no problem. However, when the value of Textboxtest is empty, the validation control does not work, the submission page succeeds. What's the reason?
Ii. Response Events
What the hell is going on here? First, after I remove the Buttontest OnClientClick event, the validation control works. Why is that? I looked at the source code for the page and found that the Buttontest control generated the following source code:
<input type= "Submit" Name= "ButtonText" value= "test" onclick= "Javascript:webform_dopostbackwithoptions" (New WebForm_ PostBackOptions ("ButtonText", "", True, "", ", false, False)" id= "ButtonText"/>
As you can see from this line of source code, the validation control generates a section of JavaScript code on the client to verify that the value in the TextBox is empty. When I added Buttontest's OnClientClick, I reviewed the source code, Buttontest control generated the following source:
<input type= "Submit" Name= "ButtonText" value= "test" onclick= "return confirm (' Are you sure you want to submit it? '); Webform_dopostbackwithoptions (New Webform_postbackoptions ("ButtonText", "", True, "", "", false, False)) "Id=" ButtonText "/>
From this line of code, you can see very clearly where the problem is, at the client, the custom JavaScript is executed first, and then in the JavaScript that is generated by the validation control, it is clear that the validation control loses any meaning in this case.
Third, response control
Knowing where the problem is, it's OK, my solution is: In executing the custom JavaScript (return confirm) (' Are you sure you want to submit it? ') before verifying that the controls on the page conform to the rules, I modified the Buttontest OnClientClick event as follows:
Copy Code code as follows:
<asp:button id= "ButtonText" runat= "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:
Copy Code code as follows:
<script language= "javascript" type= "Text/javascript" >
function Checkclientvalidate () {
Page_clientvalidate ();
if (page_isvalid) {
return true;
}else{
return false;
}
}
</script>
Run, test. Validation controls play a role. Solve the problem.
Four, PostScript
This is what I have known to ignore the problem and solution, when I found this problem, out of a cold sweat, thanks to do a rigorous server-side verification, otherwise it would be miserable. You can also see how necessary it is to specify strict server-side validation,:-). Not only does it prevent hackers from bypassing client-side validation, but it also prevents inaccurate data due to errors that they do not detect.
Note:
Page_clientvalidate (), which returns TRUE or False in the ASPX page that contains the Microsoft validation control, based on whether the user entered the operation lawfully
can be directly judged.
Copy Code code as follows:
if (Page_clientvalidate ())
{
return true;
}
Else
{
return false;
}