2010-12-12 83 views
0

我有一些驗證控件和驗證摘要。 用戶離開文本框進行驗證後,客戶端驗證顯示出來。 服務器端驗證顯示從未啓動。 驗證摘要顯示永不起作用。ValidationSummary不顯示

我做錯了什麼?

    <div class="designPhotoMiddleText" id="MiddleReg" > 
       <asp:Panel DefaultButton="linkRegister" runat="server" ID="panRegister" > 
        Jetzt kostenlos registrieren:<br /> 
        <br /> 

        <table> 
         <tr> 
          <td style="width: 120px;"> 
           Username: 
          </td> 
          <td> 
           <asp:TextBox ID="txtRegisterUsername" Width="150px" runat="server"></asp:TextBox> 
           <asp:CustomValidator ValidationGroup="Register" ID="valUsername" ControlToValidate="txtRegisterUsername" 
           OnServerValidate="IsUsernameFree" CssClass="validator" 
           runat="server" ErrorMessage="CustomValidator"></asp:CustomValidator> 
          </td> 
         </tr> 
         <tr> 
          <td> 
           Passwort: 
          </td> 
          <td> 
           <asp:TextBox ID="txtRegisterPW1" Width="150px" TextMode="Password" runat="server"></asp:TextBox> 
           <asp:RegularExpressionValidator ValidationGroup="Register" CssClass="validator" ControlToValidate="txtRegisterPW1" ID="valPasswordLenght" runat="server" ErrorMessage="Das Passwort muss mindestens 6 Zeichen haben."></asp:RegularExpressionValidator> 
          </td> 
         </tr> 
         <tr> 
          <td> 
           Passwort erneut: 
          </td> 
          <td> 
           <asp:TextBox ID="txtRegisterPW2" Width="150px" TextMode="Password" runat="server"></asp:TextBox> 
           <asp:CompareValidator CssClass="validator" ValidationGroup="Register" 
            ID="valPW" ControlToCompare="txtRegisterPW2" ControlToValidate="txtRegisterPW1" runat="server" ErrorMessage="Die eingegebenen Passwörter stimmen nicht überein."></asp:CompareValidator> 
          </td> 
         </tr> 
         <tr> 
          <td> 
           Email: 
          </td> 
          <td> 
           <asp:TextBox ID="txtRegisterEmail" Width="150px" runat="server"></asp:TextBox> 
           <asp:RegularExpressionValidator ValidationGroup="Register" CssClass="validator" ForeColor="black" 
            ControlToValidate="txtRegisterEmail" ID="valMail" runat="server" 
            ValidationExpression=".*@.*\.(com|net|org|edu|mil|at?|de?|ch?|uk?)$" 
            ErrorMessage="Bitte geben Sie eine gültige EMail-Adresse ein." 
            Display="Dynamic"></asp:RegularExpressionValidator> 
          </td> 
         </tr> 
         <tr> 
          <td> 
          </td> 
          <td> 
           <asp:CheckBox ID="chkRegsiterAGBs" runat="server" /> 
           <asp:HyperLink ID="linkAGB" Target="_blank" NavigateUrl="~/AGBs.aspx" runat="server">AGBs</asp:HyperLink> gelesen 
           <asp:CustomValidator ValidationGroup="Register" ID="valAGB" 
           OnServerValidate="IsAGBChecked" CssClass="validator" 
           runat="server" ErrorMessage="CustomValidator"></asp:CustomValidator> 
          </td> 
         </tr> 
         <tr> 
          <td colspan="2"> 
           <asp:LinkButton CausesValidation="true" ID="linkRegister" CssClass="linkWhite" runat="server" 
            onclick="linkRegister_Click">Jetzt Registrieren</asp:LinkButton> 
           <asp:ValidationSummary ValidationGroup="Register" ID="sumRegister" runat="server" 
           HeaderText="Fehler:" 
           ShowSummary="true" DisplayMode="BulletList" /> 
          </td> 
         </tr> 
        </table> 
        </asp:Panel> 
       </div> 



    public void IsUsernameFree(object source, ServerValidateEventArgs value) 
    { 
     string username = value.Value; 
     DAL.User user = DAL.UserHandling.GetUserByName(username); 
     value.IsValid = (user == null); 
    } 

    public void IsAGBChecked(object source, ServerValidateEventArgs value) 
    { 
     value.IsValid = (chkRegsiterAGBs.Checked); 
    } 

回答

2

無法確定從查看已發佈的代碼,但您是否在任何地方檢查Page的IsValid屬性?例如。在運行任何其他方法之前?

if (Page.IsValid){ 

    //Do additional processing 
    //register user etc. 
} 

此檢查將強制執行頁面上的所有驗證控件,然後應該觸發顯示驗證錯誤。

編輯:

您需要的的ValidationGroup在您的標記設置爲「註冊」定義linkRegister控制

`<asp:LinkButton CausesValidation="true" ID="linkRegister" CssClass="linkWhite" runat="server" OnClick="linkRegister_Click" ValidationGroup="Register">Jetzt Registrieren</asp:LinkButton>` 

而且,你的密碼的RegularExpressionValidator都需要有ValidationExpression屬性的設置標記:

`<asp:RegularExpressionValidator ValidationGroup="Register" 
    CssClass="validator" ControlToValidate="txtRegisterPW1" 
    ID="valPasswordLenght" runat="server" ErrorMessage="Das Passwort 
    muss mindestens 6 Zeichen haben." Validationexpression="[\w+\d+]{6,}" 
</asp:RegularExpressionValidator>` 
+0

是的,我這樣做。在linkRegister_Click上。 – Kovu 2010-12-12 10:49:27

+0

我*想*我發現你的問題 - 請參閱我上面的編輯。 – 5arx 2010-12-12 11:14:16

+0

完美,完美,謝謝。 – Kovu 2010-12-12 11:57:11

相關問題