您的位置:首页 > 编程语言 > ASP

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐