比较校验不停止回传 [英] Compare validator doesn't stop postback
问题描述
Below is my mark up.
<asp:TextBox ID="txtPatientDateOfBirth" runat="server"
CssClass="rightDivInnerControls" ClientIDMode="Static"
CausesValidation="True">
</asp:TextBox>
<asp:CompareValidator ID="cvPatientDateOfBirth" runat="server"
ErrorMessage="Enter proper date."
Type="Date" ControlToValidate="txtPatientDateOfBirth" Font-Bold="True"
Operator="DataTypeCheck"
ValidationGroup="FirstPreview">
</asp:CompareValidator>
<asp:Button ID="btnSaveChanges" runat="server"
Text="Save Changes" OnClientClick="return showFinalReviewAlert();"
CssClass="btnPrimary hideInPrint btnEditFinalReport"
ValidationGroup="FirstPreview"
onclick="btnSaveChanges_Click" ClientIDMode="Static"/>
当我将日期更改为一个格式错误立即显示我在错误信息即可。
When I change the date to a wrong format it shows me the error message immediately.
但是,当我点击按钮btnSaveChanges它做了回发。我觉得缺了点什么,因为它的它做回发。
But when I click on the button "btnSaveChanges" it does a postback. I think something is missing because of which it is doing postback.
任何人都可以帮我这个问题。我想的停止回传,如果验证失败
Can anyone please help me with the issue. I want to stop the postback if validation fails.
感谢。
推荐答案
通过返回 showFinalReviewAlert(的价值);
在的OnClientClick
按钮,你是从发生阻塞页面验证。
By returning the value of showFinalReviewAlert();
in the OnClientClick
of the button, you are blocking the page validation from happening.
这实际上是所呈现的HTML(简化观看)...
This is effectively the HTML that is being rendered (simplified for viewing)...
<input type="submit"
id="btnSaveChanges"
onclick="return showFinalReviewAlert();WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("btnSaveChanges;, "", true, "", "", false, false))"
name="btnSaveChanges">
这最重要的一点就是...
The important bit of this is...
onclick="return showFinalReviewAlert();WebForm_DoPostBackWithOptions....
它的意思是,不管什么 showFinalReviewAlert()
返回时, WebForm_DoPostBackWithOptions
将永远不会达到。但是,因为它是一个&LT;输入类型=提交方式&gt;
页面将发布包到服务器反正
What it means is that no matter what showFinalReviewAlert()
returns, the WebForm_DoPostBackWithOptions
will never be reached. However, because it is an <input type="submit">
the page will post-pack to the server anyway.
所以,如果返回值 showFinalReviewAlert
必须停止从返回值发生后回假
,你应该设置的OnClientClick
属性,因为...
So, if the return value of the showFinalReviewAlert
must stop the post-back from happening by returning the value false
, you should set the OnClientClick
attribute as this...
OnClientClick="if(showFinalReviewAlert()==false){return false;}"
在换句话说,如果 showFinalReviewAlert
收益假
然后持续回任何后期处理停止键.. 。但如果返回真正
,然后让回来后验证的发生。
In other words, if showFinalReviewAlert
return false
then stop the button from continuing any post-back processing... but if it return true
, then allow the post-back validation to take place.
在另一方面,如果 showFinalReviewAlert的结果()
没关系......只要将收益
给简单...
On the other hand, if the result of showFinalReviewAlert()
doesn't matter... simply remove the return
to give simply...
OnClientClick="showFinalReviewAlert();"
这篇关于比较校验不停止回传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!