2012-03-07 73 views
1

我有幾個<asp:Validators>和一個<asp:ValidationSummary>一個簡單的WebForms ...asp.net的ValidationSummary

當提交表單,一切正常,一個「*」的旁邊顯示的文本框無效並且ValidationSummary顯示錯誤消息。

問題出在您通過按Enter鍵或點擊鍵而失去焦點在文本框之一上。 (onChange事件)。 驗證程序的'*'如果無效則顯示,但ValidationSummary不顯示。

僅在提交正常行爲期間顯示ValidationSummary?

看起來這是因爲JS的功能,顯示摘要(ValidationSummaryOnSubmit)設計僅在提交功能「Page_ClientValidate」 稱爲但是,這是所有生成的JS等等....

<div class="wrapper"> 
    <h2> 
     <asp:Label ID="MainStatus" runat="server" CssClass="successNotification"></asp:Label> 
    </h2> 
    <form id="form1" runat="server"> 
    <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" 
     ValidationGroup="LoginUserValidationGroup" /> 
    <asp:Label ID="StatusError" runat="server" CssClass="failureNotification"></asp:Label> 
    <!-- <asp:Panel ID="pnlMyForm" runat="server" DefaultButton="LoginButton"> --> 
     <fieldset class="login" runat="server" id="FormFieldset"> 
      <legend>Account Information</legend> 
      <p> 
       <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:</asp:Label> 
       <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox> 
       <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
        CssClass="failureNotification" Display="Dynamic" ErrorMessage="User Name is required." ToolTip="User Name is required." 
        ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator> 
      </p> 
      <p id="CurrentPwd" runat="server"> 
       <asp:Label ID="CurrentPasswordLabel" runat="server" AssociatedControlID="CurrentPassword">Current Password:</asp:Label> 
       <asp:TextBox ID="CurrentPassword" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox> 
       <asp:RequiredFieldValidator ID="ConfirmPasswordValid" runat="server" ControlToValidate="CurrentPassword" 
        CssClass="failureNotification" Display="Dynamic" ErrorMessage="Current Password is required." ToolTip="Current password is required." 
        ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator> 
       <asp:CompareValidator ID="ConfirmPasswordComp" runat="server" ControlToCompare="Password" 
        ControlToValidate="CurrentPassword" CssClass="failureNotification" Display="Dynamic" 
        Operator="NotEqual" ErrorMessage="The current password and new password must be different." 
        ValidationGroup="LoginUserValidationGroup">*</asp:CompareValidator> 
      </p> 
      <p id="Pwd" runat="server"> 
       <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password" Height="21px">New Password:</asp:Label> 
       <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox> 
       <asp:RequiredFieldValidator Display="Dynamic" ID="PasswordRequired" runat="server" ControlToValidate="Password" 
        CssClass="failureNotification" ErrorMessage="New Password is required." ToolTip="New Password is required." 
        ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator> 
      </p> 
      <p id="ConfirmPwd" runat="server"> 
       <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label> 
       <asp:TextBox ID="ConfirmPassword" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox> 
       <asp:RequiredFieldValidator ControlToValidate="ConfirmPassword" CssClass="failureNotification" 
        Display="Dynamic" ErrorMessage="Confirmation Password is required." ID="ConfirmPasswordRequired" 
        runat="server" ToolTip="Confirmation Password is required." ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator> 
       <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password" 
        ControlToValidate="ConfirmPassword" CssClass="failureNotification" Display="Dynamic" 
        ErrorMessage="The password was not correctly confirmed. Please ensure that the new password and confirmed password match exactly." 
        ValidationGroup="LoginUserValidationGroup">&nbsp</asp:CompareValidator> 
      </p> 
     </fieldset> 
     <p class="right"> 
      <input id="ResetButton" runat="server" type="reset" value="Clear fields" /> 
      <asp:Button ID="LoginButton" runat="server" Text="Update Password" OnClick="Button_Update_Pwd" 
       ValidationGroup="LoginUserValidationGroup" /> 
      <asp:Button ID="UsrButton" runat="server" Text="Next" OnClick="Button_Check_User" 
       ValidationGroup="LoginUserValidationGroup" /> 
     </p> 
     <!-- </asp:Panel> --> 
    </form> 
</div> 

回答

0
Is showing the ValidationSummary ONLY during a submit a normal behavior ? 

是的。驗證摘要就是這樣工作的。您可以隨時在需要時自行調用Page_ClientValidate函數。但是,它會評估整個頁面,包括您還沒有填寫的字段,這些字段也包含必需的字段驗證程序,因此請確保這是您要執行的操作。

1

這是我的解決方法:

存儲最後聚焦的表單字段與滾動條位置的每一個表單域獲得焦點,如時間沿:

<input id="Text1" type="text" onfocus="SetPageActiveElement(this);" /> 

<select id="Select1" onfocus="SetPageActiveElement(this);"> 
    <option>option 1</option> 
</select> 

上模糊事件中使用(平變化的選擇)調用Page_ClientValidate()並強制驗證摘要來刷新其內容。以防萬一,也清除最後關注的模糊元素。所以,你最終不得不像:

<input id="Text1" type="text" onfocus="SetPageActiveElement(this);" onblur="ClearValidationSummary();" /> 

<select id="Select1" onfocus="SetPageActiveElement(this);" onchange="ClearValidationSummary();"> 
    <option>option 1</option> 
</select> 

這些都是我的JS功能:

var activeElement; 

// This function saves the last element which got focus and saves it on a variable to use it later 
// Same thing applies for vertical and horizontal scroll position 
function SetPageActiveElement(element) { 
    activeElement = element; 
} 

function ClearValidationSummary() { 
    var vscroll = (document.all ? document.scrollTop : window.pageYOffset); 
    var hscroll = (document.all ? document.scrollLeft : window.pageXOffset); 
    // Forces page validation to refresh validation summaries 
    Page_ClientValidate(); 

    if (activeElement != null) { 
     // Returns the last vertical and horizontal scroll bar position 
     window.scrollTo(hscroll, vscroll); 

     SetPageActiveElement(null); 
    } 
} 

希望它可以幫助別人。

相關問題