asp.net CustomValidator never fires OnServerValidate(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:asp.net CustomValidator 从不触发 OnServerValidate
基础教程推荐
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- 创建属性设置器委托 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01