简体   繁体   English

比较验证器不会停止回发

[英]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. 当我将日期更改为错误的格式时,它会立即显示错误消息

在此输入图像描述

But when I click on the button "btnSaveChanges" it does a postback. 但是,当我单击“ btnSaveChanges”按钮时,它会回发。 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 . 如果验证失败,我想停止回发

Thanks. 谢谢。

Actually, the way these ASP.NET Validators work (described here: Validating User Input in ASP.NET Web Pages ) is a bit different from what you're expecting - it's supposed to postback. 实际上,这些ASP.NET验证程序的工作方式(在此处进行说明: 验证ASP.NET网页中的用户输入 )与您期望的有所不同-应该回发。 From the linked MSDN document: 从链接的MSDN文档中:

When the user submits a page to the server, the validation controls are invoked to check the user input, control by control. 当用户向服务器提交页面时,将通过控件来调用验证控件以检查用户输入。 If a validation error is detected in any of the input controls, the page itself is set to an invalid state so you can test for validity before your code runs. 如果在任何输入控件中检测到验证错误, 则页面本身将设置为无效状态,因此您可以在代码运行之前测试有效性。

Emphasis mine. 强调我的。 When they say "set the page to an invalid state", they are referring to the Page.IsValid property. 当他们说“将页面设置为无效状态”时,他们指的是Page.IsValid属性。 So, when you're server-side code runs, you would need to wrap it in a block like this: 因此,当您运行服务器端代码时,需要将其包装在这样的块中:

if(Page.IsValid)
{
    // Run code now that validation has been verified.
}

In order to prevent a postback entirely, you would need to just use JavaScript (client-side code) in order to disable the submit button until all of your controls are in a valid state. 为了完全防止回发,您只需要使用JavaScript(客户端代码)即可禁用“提交”按钮,直到所有控件均处于有效状态为止。

By returning the value of showFinalReviewAlert(); 通过返回showFinalReviewAlert();的值showFinalReviewAlert(); in the OnClientClick of the button, you are blocking the page validation from happening. 在按钮的OnClientClick中,您将阻止进行页面验证。

This is effectively the HTML that is being rendered (simplified for viewing)... 这实际上是正在呈现的HTML(为查看而简化)...

<input type="submit" 
       id="btnSaveChanges" 
       onclick="return showFinalReviewAlert();WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;btnSaveChanges;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" 
       name="btnSaveChanges">

The important bit of this is... 重要的一点是...

onclick="return showFinalReviewAlert();WebForm_DoPostBackWithOptions....

What it means is that no matter what showFinalReviewAlert() returns, the WebForm_DoPostBackWithOptions will never be reached. 这意味着无论showFinalReviewAlert()返回什么, WebForm_DoPostBackWithOptions将永远不会到达WebForm_DoPostBackWithOptions However, because it is an <input type="submit"> the page will post-pack to the server anyway. 但是,由于它是<input type="submit">因此页面无论如何都会后包装到服务器。

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... 因此,如果showFinalReviewAlert的返回值必须通过返回false值来阻止回发,则应将OnClientClick属性设置为...

OnClientClick="if(showFinalReviewAlert()==false){return false;}"

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返回false则停止按钮继续执行任何回发处理...但是,如果返回true ,则允许进行回发验证。

On the other hand, if the result of showFinalReviewAlert() doesn't matter... simply remove the return to give simply... 另一方面,如果showFinalReviewAlert()的结果无关紧要,只需删除return即可得出...

OnClientClick="showFinalReviewAlert();"

Add OnClientClick as well as Onclick event in your server control asp:button, 在您的服务器控件asp:button中添加OnClientClick以及Onclick事件,

asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_OnClick" OnClientClick ="if(validateTotal()==false){return false;}" asp:按钮 ID =“ btnSave” runat =“服务器” Text =“ Save” OnClick =“ btnSave_OnClick” OnClientClick =“ if(validateTotal()== false){返回false;}”

If function OnClientClick event will return false then it will not postback. 如果函数OnClientClick事件将返回false,则不会回发。

your validateTotal() may be like this, 您的validateTotal()可能像这样,

function validateTotal() 
{
    if (parseInt($('.lblTotal').text().trim()) > 100) {
        alert("Please check total value, it should not be more than 100%");
        return false;
    }
}

You're missing ValidationGroup in the TextBox control. 您在TextBox控件中缺少ValidationGroup

<asp:TextBox ID="txtPatientDateOfBirth" runat="server" 
    CssClass="rightDivInnerControls" ClientIDMode="Static" 
    CausesValidation="True"
    ValidationGroup="FirstPreview">
</asp:TextBox>

It is because you have used: 这是因为您使用了:

OnClientClick="return showFinalReviewAlert();"

What you are doing in showFinalReviewAlert();" ? 您在showFinalReviewAlert();"正在做什么?

What you can do inside showFinalReviewAlert();" is add return false that will stop Postback of the page. 您可以在showFinalReviewAlert();"内部进行的showFinalReviewAlert();"是添加return false ,这将停止页面的回发。

<script language="javascript">
    function showFinalReviewAlert() {
        //
        //   Your coding stuffs...
        //
        return false;
    }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM