1. Event
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:
The .Net page is as follows:
Copy the 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="server" Text="Test" OnClientClick="return confirm('Are you sure you want to submit?');" />
</div>
</form>
As above, add the RequireFieldValidator validation control to the page so that the value of TextBoxTest cannot be empty. When ButtonText submits the page, the user is asked to confirm whether it needs to be submitted. 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. Respond 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 generates 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 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:
<input type="submit" name="ButtonText" value="Test" onclick="return confirm('Are you sure you want to submit?');WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(ButtonText, , true, , , false, false)) " id="ButtonText" />
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, in this case, , the validation control loses any meaning.
3. Response controls
Once you know where the problem is, it will be easier to solve. My solution is: before executing the custom javascript (return confirm('Are you sure you want to submit?'), verify whether the controls on the page comply with the rules. , so I modified the OnClientClick event of ButtonTest as follows:
Copy the code code as follows:
<asp:Button ID="ButtonText" runat="server" Text="Test" OnClientClick="if(CheckClientValidate()) return Confirm('Are you sure you want to submit the page?');" />
The code of the CheckClientValidate() method is as follows:
Copy the 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 work. Problem solved.
4. Postscript
This is the 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 based on whether the user input operation is legal.
Can be judged directly.
Copy the code code as follows:
if(Page_ClientValidate())
{
return true;
}
else
{
return false;
}