asp.net CustomValidator 从不触发 OnServerValidate

发布于 2024-08-09 16:28:38 字数 2828 浏览 6 评论 0原文

我有以下 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

I have the following ASP page:

<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>

and the following code behind for the CustomValidator1_ServerValidate() method:

    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!";
        }

    }

It never seems to fail validation no matter what I put in the text box...

Should mention this is ASP.NET 2.0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

素食主义者 2024-08-16 16:28:38

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

否则所需的字段验证将不起作用。

When you are testing if the textbox is empty, use this ValidateEmptyText="true" on the CustomValidator.

Otherwise required field validation won't work.

倾城泪 2024-08-16 16:28:38

为了使用自定义验证器,您还需要同一控件的 requiredfieldvalidator。只需为 NoteDateTextBox 添加一个 requiredfieldvalidator,它就会为您触发 customvalidator 的服务器事件。

In order to use a customvalidator, you also need a requiredfieldvalidator for that same control. Just put a requiredfieldvalidator for NoteDateTextBox and it should fire the customvalidator's server event for you.

当爱已成负担 2024-08-16 16:28:38

要添加到 Dan 的响应中,使用 CustomValidator 的另一种方法是:

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

前面

的示例

<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";
}

To add to Dan's response, an alternative way of using a CustomValidator is:

  • Remove the ControlToValidate property
  • In your OnServerValidate method, reference the control you are validating instead of using ServerValidateEventArgs.Value, e.g.

Example

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" />

Code behind

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";
}
酒绊 2024-08-16 16:28:38

我遇到了类似的问题,空白条目未经过验证;把它放在这里,因为这是我搜索的内容。

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

I had a similar issue for which blank entries were not validated; putting it here because this is what I searched on.

My solution was the attribute: ValidateEmptyText="true"

坦然微笑 2024-08-16 16:28:38

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

Make sure the validator is Visible when you invoke the validation. In my case, I only set a parent control to Visible during DataBind, which was too late. The validator inherited the Visible value and was always considered valid.
This is a requirement for any server-side validator, by the way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文