The validation mechanism of asp.net
I wonder that, what is the process of the validation mechanism in asp.net? When I donot enter anything in the textboxes, the error messages come but which I wonder the mechanism of it, which attribute triggers it?
<form id="form1" runat="server">
Your name:<br />
<asp:TextBox runat="server" id="txtName" />
<asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="txtName" errormessage="Please enter your name!" />
<开发者_StackOverflow社区;br /><br />
<asp:Button runat="server" id="btnSubmitForm" text="Ok" />
which mechanism supplies the control in asp.net?
You should search the documentation about the RequiredFieldValidator
control.
I am convinced that control is working checking the content of the linked control (specified by id in the controltovalidate
attribute) and rendering the error in case the control fails to validate.
what other details would you like to know?
When you use required field validators, JavaScript methods are added to the page. You can see how the validators work by looking at the page source:
<script type="text/javascript">
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
var Page_Validators = new Array(document.getElementById("ctl00_ContentBody_reqBilledDate"), document.getElementById("ctl00_ContentBody_reqInvoiceNumber"), document.getElementById("ctl00_ContentBody_reqBillingMethod"));
//]]>
</script>
<script type="text/javascript">
var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
ValidatorOnLoad();
}
function ValidatorOnSubmit() {
if (Page_ValidationActive) {
return ValidatorCommonOnSubmit();
}
else {
return true;
}
}
</script>
精彩评论