asp.net CustomValidator永远不会触发OnServerValidate

时间:2009-10-30 17:16:22

标签: asp.net validation .net-2.0 webforms

我有以下ASP页面:

<asp:Content ID="Content2" ContentPlaceHolderID="ShellContent" runat="server">
    <form runat="server" id="AddNewNoteForm" method="post"">

        <fieldset id="NoteContainer">
            <legend>Add New Note</legend>
            <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
            <div class="ctrlHolder">
                <asp:Label ID="LabelNoteDate" runat="server" Text="Note Date" 
                    AssociatedControlID="NoteDateTextBox"></asp:Label>
                <asp:TextBox ID="NoteDateTextBox" runat="server" class="textInput" 
                    CausesValidation="True" ></asp:TextBox>
                <asp:CustomValidator 
                        ID="CustomValidator1" 
                        runat="server" 
                        ErrorMessage="CustomValidator" 
                        ControlToValidate="NoteDateTextBox" 
                        OnServerValidate="CustomValidator1_ServerValidate" 
                        Display="Dynamic" 
                        >*</asp:CustomValidator>
            </div>
            <div class="ctrlHolder">
                <asp:Label ID="LabelNoteText" AssociatedControlID="NoteTextTextBox" runat="server" Text="Note"></asp:Label>
                <asp:TextBox ID="NoteTextTextBox" runat="server" Height="102px" 
                    TextMode="MultiLine" class="textInput" ></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
                    ErrorMessage="Note Text is Required" ControlToValidate="NoteTextTextBox">*</asp:RequiredFieldValidator>   

            </div>
            <div class="buttonHolder">
                <asp:Button ID="OkButton" runat="server" Text="Add New Note"  
                    CssClass="primaryAction" onclick="OkButton_Click"/>
                <asp:HyperLink ID="HyperLink1" runat="server">Cancel</asp:HyperLink>
            </div>
        </fieldset>
    </form>
</asp:Content>

以及CustomValidator1_ServerValidate()方法的后面的代码:

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {

        if (string.IsNullOrEmpty(args.Value.Trim()))
        {
            args.IsValid = false;
            CustomValidator1.ErrorMessage = "Note Date is Required!";
            return;
        }

        DateTime testDate;
        if (DateTime.TryParse(args.Value, out testDate))
        {
            args.IsValid = true;
            CustomValidator1.ErrorMessage = "Invalid Date!";
        }

    }

无论我在文本框中放置什么,似乎永远都不会失败验证...

应该提到这是ASP.NET 2.0

5 个答案:

答案 0 :(得分:15)

当您测试文本框是否为空时,请在CustomValidator上使用此ValidateEmptyText =“true”。

否则必填字段验证将无效。

答案 1 :(得分:10)

为了使用customvalidator,您还需要一个针对该控件的requiredfieldvalidator。只需为NoteDateTextBox添加一个requiredfieldvalidator,就可以为你激活customvalidator的服务器事件。

答案 2 :(得分:7)

要添加Dan的回复,使用CustomValidator的另一种方法是:

  • 删除ControlToValidate属性
  • OnServerValidate方法中,引用您正在验证的控件,而不是使用ServerValidateEventArgs.Value,例如

实施例

Code infront

<asp:ValidationSummary runat="server" DisplayMode="BulletList" ValidationGroup="form" />

<asp:TextBox runat="server" ID="_textbox"/>
<asp:CustomValidator runat="server" 
        ErrorMessage="Please enter the secret" 
        OnServerValidate="TextBoxValidate"
        ValidationGroup="form" 
        Display="None"
        EnableClientScript="false" />
<asp:button runat="server" OnClick="ButtonClick" Text="Press" />

背后的代码

protected void ButtonClick(object sender, EventArgs e)
{
    Page.Validate();

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

protected void TextBoxValidate(object sender, ServerValidateEventArgs args)
{
    args.IsValid = _textbox.Text == "secret";
}

答案 3 :(得分:4)

我有一个类似的问题,没有验证空白条目;把它放在这里,因为这是我搜索的内容。

我的解决方案是属性:ValidateEmptyText =“true”

答案 4 :(得分:0)

调用验证时,请确保验证程序为Visible。就我而言,我只在Visible期间将父控件设置为DataBind,这为时已晚。验证器继承了Visible值,并始终被视为有效。 顺便说一句,这是任何服务器端验证器的要求。