Client function doesn't works if you set the ASP.NET server control AutoPostBack to 'True'.
2009-09-01 11:53
555 查看
Acutually, the Client validate function also works as loog as the OnTextChanged event was fired. Only the Client validate works before the Post back caused by OnTextChanged.
In fact, when you drag the ASP.NET validator control to the page, when you run the page, you will find that the form has be added a property(Onsubmit) and assign 'javascript:return WebForm_OnSubmit();' to this property. Such as follows:
<form name="form1" method="post" action="Test6.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
Meanwhile, please go ahead and tip the related JavaScript function.
<script type="text/javascript">
//<![CDATA[
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
//]]>
</script>
And also please notice the __doPostBack function code segment which be generated due to the AutoPostBack property of TextBox has been set to 'True'.
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
By now, you will found that the __doPostBack function will be fired before the WebForm_OnSubmit() funtion. Hence, the situation has happend like you describled.
So, please refer to the following code to solve your problem.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body >
<form id="form1" runat="server">
<script type="text/javascript">
function __doPostBack(eventTarget, eventArgument) {
if (typeof (Page_ClientValidate) == 'function') {
Page_ClientValidate();
if (Page_IsValid)
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
return Page_IsValid;
}
else {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
return true;
}
}
</script>
<asp:TextBox ID="DiscountRate" runat="server" Style="float: none; width: 50px;"
OnTextChanged="UpdateDiscountedCourseFeeOnTextChanged" AutoPostBack="true" CausesValidation="true" />%
<asp:RangeValidator ID="DiscountRateRangeValidator" runat="server" ControlToValidate="DiscountRate"
MinimumValue="0.00" MaximumValue="100.00" Type="Double" ErrorMessage="Discount Rate must be within 0-100"
ToolTip="Discount Rate must be within 0-100" ValidationGroup="PaymentGroup" SetFocusOnError="true" Display="dynamic">*</asp:RangeValidator>
</form>
</body>
</html>
In fact, when you drag the ASP.NET validator control to the page, when you run the page, you will find that the form has be added a property(Onsubmit) and assign 'javascript:return WebForm_OnSubmit();' to this property. Such as follows:
<form name="form1" method="post" action="Test6.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
Meanwhile, please go ahead and tip the related JavaScript function.
<script type="text/javascript">
//<![CDATA[
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
//]]>
</script>
And also please notice the __doPostBack function code segment which be generated due to the AutoPostBack property of TextBox has been set to 'True'.
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
By now, you will found that the __doPostBack function will be fired before the WebForm_OnSubmit() funtion. Hence, the situation has happend like you describled.
So, please refer to the following code to solve your problem.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body >
<form id="form1" runat="server">
<script type="text/javascript">
function __doPostBack(eventTarget, eventArgument) {
if (typeof (Page_ClientValidate) == 'function') {
Page_ClientValidate();
if (Page_IsValid)
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
return Page_IsValid;
}
else {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
return true;
}
}
</script>
<asp:TextBox ID="DiscountRate" runat="server" Style="float: none; width: 50px;"
OnTextChanged="UpdateDiscountedCourseFeeOnTextChanged" AutoPostBack="true" CausesValidation="true" />%
<asp:RangeValidator ID="DiscountRateRangeValidator" runat="server" ControlToValidate="DiscountRate"
MinimumValue="0.00" MaximumValue="100.00" Type="Double" ErrorMessage="Discount Rate must be within 0-100"
ToolTip="Discount Rate must be within 0-100" ValidationGroup="PaymentGroup" SetFocusOnError="true" Display="dynamic">*</asp:RangeValidator>
</form>
</body>
</html>
相关文章推荐
- How do I set the timeout for a JAX-WS webservice client? if you want to set timeouts for specific se
- ASP.NET makes uploading files from the client to the server a snap(UploadInterface.PostedFile.SaveAs)
- The 4 server logs you NEED to know to fix any IIS / ASP.NET error
- CodeSnip: How to Get Id of the Record Using ASP.NET and SQL Server 2000
- Error>: CGContextDrawImage: invalid context 0x0. If you want to see the backtrace, please set CG_CON
- 4 ways to send a PDF file to the IE Client in ASP.NET 2.0
- 苹果官方解释可以忽略『CGBitmapContextCreateImage: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE...』报错的帖子
- asp.net mysql无法查询中文;You have an error in your SQL syntax; check the manual that corresponds to
- Using the Index Server to create Query Page in asp.net
- <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_C
- ASP.NET AJAX Roadmap--Getting Started (6): Introduction to the Timer Control
- 4 ways to send a PDF file to the IE Client in ASP.NET 2.0
- Basic Sample - How To Keep ASP.NET ViewState On The Server
- ASP.Net DebugError解决方案[转]:Unable to start debugging on the web server.Debugging failes because integrated Windows authentication is not enabled.
- 使用asp.net urlrewrite 出现 Session state can only be used when enableSessionState is set to true 错误
- Link ASP.NET Data Grid to Microsoft Excel in the client side
- HOW TO Set Up Multi-Server ASP.NET Web Applications and Web Services
- SQL Server Setup failed to obtain system account information for the ASPNET
- There is no tracking information for the current branch.If you wish to set tracking information for
- Downloading files from a server to client, using ASP.Net, when file size is too big for MemoryStream using Generic Handlers (ashx)