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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您测试文本框是否为空时,请在 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.
为了使用自定义验证器,您还需要同一控件的 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.
要添加到 Dan 的响应中,使用
CustomValidator
的另一种方法是:ControlToValidate
属性OnServerValidate
方法中,引用您所在的控件验证而不是使用ServerValidateEventArgs.Value
,例如前面
的示例
代码后面的代码
To add to Dan's response, an alternative way of using a
CustomValidator
is:ControlToValidate
propertyOnServerValidate
method, reference the control you are validating instead of usingServerValidateEventArgs.Value
, e.g.Example
Code infront
Code behind
我遇到了类似的问题,空白条目未经过验证;把它放在这里,因为这是我搜索的内容。
我的解决方案是属性: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"
当您调用验证时,请确保验证器是
可见
。就我而言,我仅在DataBind
期间将父控件设置为Visible
,但为时已晚。验证器继承了Visible
值并始终被认为是有效的。顺便说一句,这是任何服务器端验证器的要求。
Make sure the validator is
Visible
when you invoke the validation. In my case, I only set a parent control toVisible
duringDataBind
, which was too late. The validator inherited theVisible
value and was always considered valid.This is a requirement for any server-side validator, by the way.