ValidationSummary没有出现

时间:2010-12-12 10:04:08

标签: asp.net validation

我有一些带验证的验证控件。 在用户离开文本框进行验证后,将显示客户端验证。 服务器端验证显示从未启动。 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);
    }

1 个答案:

答案 0 :(得分: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 for password需要在标记中设置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>`