用于唯一条目的ASP.NET自定义验证程序不起作用

时间:2016-06-07 10:29:05

标签: asp.net vb.net customvalidator custom-validators

我正在尝试实现一个自定义验证器来共同映射数据库中的值,以便条目是唯一的 - 在我的注册页面中。

这是我的aspx页面

public class Selezione {
    JFrame frame;
}

vb.net页面中的服务器端代码是

    <h4>  Sign up  </h4>

    <div class="">
        <div class="status">
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableViewState="false"
                ValidationGroup="EmployerRegister" HeaderText="Sorry we could not process request because:"
                CssClass="error" />
        </div>

        <p class="input-block last">
            <label for="<%=uxEmployerName.ClientId %>">
                <strong>Company Name</strong> (required)
            </label>

            <asp:TextBox ID="uxEmployerName" runat="server" CssClass="uxemail" TabIndex="10"
                ValidationGroup="EmployerRegister" />
            <asp:RequiredFieldValidator ID="uxEmployerNameValReq" runat="server" ControlToValidate="uxEmployerName"
                ErrorMessage="You must enter Company Name." EnableClientScript="true"  Display="Dynamic"
                ValidationGroup="EmployerRegister" />
        </p>


        <p class="input-block last">
            <label for="<%=uxEmail.ClientId %>"> <strong>Your Email</strong> (required) </label>

            <asp:TextBox ID="uxEmail" runat="server" CssClass="uxemail" TabIndex="16" ValidationGroup="EmployerRegister" />

            <asp:RequiredFieldValidator ID="uxEmailValReq" runat="server" ControlToValidate="uxEmail"
                ErrorMessage="You must enter Email." EnableClientScript="true" Display="none"
                ValidationGroup="EmployerRegister" />

            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="The 'Email Address' you have entered does not appear to be a valid address."
                ControlToValidate="uxEmail" EnableViewState="False" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
                ValidationGroup="EmployerRegister" Display="None" />

            <asp:CustomValidator ID="uxEmailValUnique" runat="server" OnServerValidate="uxEmailValUnique_ServerValidate"  ErrorMessage="It appears that you already have an account with us."
                ControlToValidate="uxEmail" EnableViewState="False" ValidationGroup="EmployerRegister"
                Display="None" />

        </p>


        <p class="input-block last">
            <asp:LinkButton runat="server" ID="uxRegisterButton" Text="Submit" CausesValidation="true"
                ValidationGroup="EmployerRegister" CssClass="button" TabIndex="18" />

        </p>
    </div>

LocalHelper文件中的函数是

Private Sub uxEmailValUnique_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles uxEmailValUnique.ServerValidate
    args.IsValid = Not LocalHelper.EmailExists(uxEmail.Text)
End Sub

但是自定义验证器不起作用。它采取重复的电子邮件地址,并没有给出任何错误消息我的数据库已正确连接。

此代码中有什么问题/如何解决此问题。

1 个答案:

答案 0 :(得分:1)

为你的html,控件的CustomValidator中缺少的ServerValidate event创建一个事件处理程序,如下所示: -

<asp:CustomValidator id="CustomValidator1"
           ControlToValidate="Text1"
           Display="Static"
           ErrorMessage="Not an even number!"
           ForeColor="green"
           Font-Names="verdana" 
           Font-Size="10pt"
           OnServerValidate="ServerValidation"
           runat="server"/>

如果这不起作用,请在submit event上尝试: -

Page.Validate()                
If Page.IsValid Then
   'your code 
End If

您的自定义验证程序ServerValidate代码

Protected Sub CustomValidator1_ServerValidate(source As Object, args As ServerValidateEventArgs)
    args.IsValid = False
End Sub

希望这会对你有所帮助。