簡體   English   中英

UpdatePanel中的CompareValidator-VS2008

[英]CompareValidator inside an UpdatePanel - VS2008

我正在使用UpdatePanel,並希望在兩個文本框中放置一個CompareValidator,以驗證用戶輸入的密碼和確認是否相同。

開箱即用,可以很好地工作(我有VS2008,並且正在使用.NET 3.5),但有一個小問題:

用戶單擊第一個文本框后,便有機會在第二個文本框中鍵入內容,從而觸發驗證。 這不會以編程方式引起任何真正的問題(所有發生的都是錯誤消息顯示,當他們輸入確認時消失了),但是我們的測試人員說這是一個問題。 直到用戶單擊“保存”,驗證才會觸發,UA不會通過UA測試。

我如何讓CompareValidator在兩個框內都輸入文本之前不觸發?

編輯:

這是標記的示例。

    <div>
        <div><asp:Label runat="server" ID="lblPassword" Text="Password"/></div>
        <div><asp:TextBox runat="server" TextMode="password" ID="txtPassword" size="25" /></div>    
    </div>
    <div>
        <div><asp:Label runat="server" ID="lblConfirmPassword" Text="Confirm Password"/></div>
        <div><asp:TextBox runat="server" TextMode="password" ID="txtConfirmPassword" size="25" /></div>
    </div>
    <asp:CompareValidator ID="CompareValidator1" ValidationGroup="PublishPassValidation" ControlToValidate="txtPassword" ControlToCompare="txtConfirmPassword" runat="server" ErrorMessage="Passwords do not match"></asp:CompareValidator>

上面的控件位於頁面上UpdatePanel的ContentTemplate中。

(為簡潔起見,刪除了CSS類和樣式)

您可以關閉該驗證程序的客戶端驗證。

EnableClientScript="false"

但是,這將意味着往返於服務器以報告無效狀態,並且您必須確保在繼續之前檢查頁面是否確實有效。

Page.Validate("PublishPassValidation");

if (Page.IsValid)
{
    // Do Stuff
}

我感覺您有孩子作為更新面板上啟用的觸發器嗎?

用戶是否在密碼框中按“ ENTER”? 您能否確認由於某種原因,更新面板在移動焦點后是否正在執行部分刷新?

如果是這樣,它將觸發驗證。

嘗試切換它,以便在確認文本框而不是密碼文本框上進行驗證。 這樣,只有在您修改確認文本框或提交表單后,它才會觸發。 並且您可能希望在密碼文本框中具有必需的字段驗證器。

<div>
    <div><asp:Label runat="server" ID="lblPassword" Text="Password"/></div>
    <div><asp:TextBox runat="server" TextMode="password" ID="txtPassword" size="25" />
         <asp:RequiredFieldValidator runat="server" ID="passwordRequiredValidator"
                                     ControlToValidate="txtPassword"
                                     ValidationGroup="PublishPassValidation"
                                     ErrorMessage="Password is required."  />    
    </div>    
</div>
<div>
    <div><asp:Label runat="server" ID="lblConfirmPassword" Text="Confirm Password"/></div>
    <div><asp:TextBox runat="server" TextMode="password" ID="txtConfirmPassword" size="25" /></div>
</div>
<asp:CompareValidator ID="CompareValidator1" ValidationGroup="PublishPassValidation"
                      ControlToValidate="txtConfirmPassword"
                      ControlToCompare="txPassword" runat="server"
                      ErrorMessage="Passwords do not match">
</asp:CompareValidator>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM