本文介绍了asp.net CustomValidator 从不触发 OnServerValidate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 ASP 页面:

<form runat="server" id="AddNewNoteForm" method="post""><fieldset id="NoteContainer"><legend>添加新笔记</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:自定义验证器ID="CustomValidator1"runat="服务器"ErrorMessage="CustomValidator"ControlToValidate="NoteDateTextBox"OnServerValidate="CustomValidator1_ServerValidate"显示=动态">*</asp:CustomValidator>

<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 class="buttonHolder"><asp:Button ID="OkButton" runat="server" Text="添加新注释"CssClass="primaryAction" onclick="OkButton_Click"/><asp:HyperLink ID="HyperLink1" runat="server">取消</asp:HyperLink>

</fieldset></表单></asp:内容>

以及用于 CustomValidator1_ServerValidate() 方法的以下代码:

 protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args){if (string.IsNullOrEmpty(args.Value.Trim())){args.IsValid = false;CustomValidator1.ErrorMessage = "需要注意日期!";返回;}日期时间测试日期;if (DateTime.TryParse(args.Value, out testDate)){args.IsValid = true;CustomValidator1.ErrorMessage = "无效日期!";}}

无论我在文本框中输入什么,它似乎都不会失败...

应该说这是ASP.NET 2.0

解决方案

为了使用 customvalidator,您还需要一个 requiredfieldvalidator 用于同一控件.只需为 NoteDateTextBox 放置一个 requiredfieldvalidator ,它就会为您触发 customvalidator 的服务器事件.

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

解决方案

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.

这篇关于asp.net CustomValidator 从不触发 OnServerValidate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 05:53