asp.net CustomValidator从不触发OnServerValidate

前端之家收集整理的这篇文章主要介绍了asp.net CustomValidator从不触发OnServerValidate前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下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

解决方法

为了使用customvalidator,同样的控件也需要一个requiredfieldvalidator.只需为NoteDateTextBox放置一个requiredfieldvalidator,它应该为您启动customvalidator的服务器事件.
原文链接:https://www.f2er.com/aspnet/250194.html

猜你在找的asp.Net相关文章