ASP.NET 自定义验证器客户端和 服务器端验证未触发
我以前没有发生过这种情况,但由于某种原因,客户端和服务器端验证事件都没有被触发:
<asp:TextBox ID="TextBoxDTownCity" runat="server" CssClass="contactfield" />
<asp:CustomValidator ID="CustomValidator2" runat="server" EnableClientScript="true"
ErrorMessage="Delivery Town or City required"
ClientValidationFunction="TextBoxDTownCityClient"
ControlToValidate="TextBoxDTownCity"
OnServerValidate="TextBoxDTownCity_Validate" Display="Dynamic" >
</asp:CustomValidator>
服务器端验证事件:
protected void TextBoxDTownCity_Validate(object source, ServerValidateEventArgs args)
{
args.IsValid = false;
}
客户端验证事件:
function TextBoxDCountyClient(sender, args) {
args.IsValid = false;
alert("test");
}
我认为至少服务器端验证会触发,但没有。 我以前从未发生过这种情况。 这实在是让我难住了。
我查看了输出,ASP.NET 正在识别客户端功能:
ASP.NET JavaScript 输出:
var ctl00_ctl00_content_content_CustomValidator2 = document.all ? document.all["ctl00_ctl00_content_content_CustomValidator2"] : document.getElementById("ctl00_ctl00_content_content_CustomValidator2");
ctl00_ctl00_content_content_CustomValidator2.controltovalidate = "ctl00_ctl00_content_content_TextBoxDTownCity";
ctl00_ctl00_content_content_CustomValidator2.errormessage = "Delivery Town or City required";
ctl00_ctl00_content_content_CustomValidator2.display = "Dynamic";
ctl00_ctl00_content_content_CustomValidator2.evaluationfunction = "CustomValidatorEvaluateIsValid";
ctl00_ctl00_content_content_CustomValidator2.clientvalidationfunction = "TextBoxDTownCityClient";
渲染的自定义验证器:
<span id="ctl00_ctl00_content_content_CustomValidator2" style="color:Red;display:none;">Delivery Town or City required</span>
任何人都可以阐明为什么客户端和服务器端验证都不会被触发。
编辑:拼写错误,我粘贴了错误的函数,问题仍然相同
只是对最后一条评论的另一个更新:文本框不能为空。 我对此进行了测试,但事实并非如此。 在空白页面上,CustomValidator 很好地触发了我的客户端验证函数,但没有值:
<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="CustomValidator" ClientValidationFunction="TextBoxDAddress1Client"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
This has not happened to me before, but for some reason both the client and server side validation events are not being triggered:
<asp:TextBox ID="TextBoxDTownCity" runat="server" CssClass="contactfield" />
<asp:CustomValidator ID="CustomValidator2" runat="server" EnableClientScript="true"
ErrorMessage="Delivery Town or City required"
ClientValidationFunction="TextBoxDTownCityClient"
ControlToValidate="TextBoxDTownCity"
OnServerValidate="TextBoxDTownCity_Validate" Display="Dynamic" >
</asp:CustomValidator>
Server-side validation event:
protected void TextBoxDTownCity_Validate(object source, ServerValidateEventArgs args)
{
args.IsValid = false;
}
Client-side validation event:
function TextBoxDCountyClient(sender, args) {
args.IsValid = false;
alert("test");
}
I thought at the least the Server Side validation would fire but no. this has never happened to me before. This has really got me stumped.
I looked at the output and ASP.NET is recognizing the client side function:
ASP.NET JavaScript output:
var ctl00_ctl00_content_content_CustomValidator2 = document.all ? document.all["ctl00_ctl00_content_content_CustomValidator2"] : document.getElementById("ctl00_ctl00_content_content_CustomValidator2");
ctl00_ctl00_content_content_CustomValidator2.controltovalidate = "ctl00_ctl00_content_content_TextBoxDTownCity";
ctl00_ctl00_content_content_CustomValidator2.errormessage = "Delivery Town or City required";
ctl00_ctl00_content_content_CustomValidator2.display = "Dynamic";
ctl00_ctl00_content_content_CustomValidator2.evaluationfunction = "CustomValidatorEvaluateIsValid";
ctl00_ctl00_content_content_CustomValidator2.clientvalidationfunction = "TextBoxDTownCityClient";
Rendered custom validator:
<span id="ctl00_ctl00_content_content_CustomValidator2" style="color:Red;display:none;">Delivery Town or City required</span>
Can any one shed some light as to why both client and server side validation would not be firing.
Edit: Typo I pasted in the wrong function, problem still the same
Just another update to the last comment: where by the TextBox cannot be empty. I tested this out and it is not true. On a blank page the CustomValidator fired my client side validation function fine without a value:
<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="CustomValidator" ClientValidationFunction="TextBoxDAddress1Client"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用此:
验证空字段。
您不需要添加 2 个验证器!
Use this:
To validate an empty field.
You don't need to add 2 validators !
仅当
TextBox
不为空时,您的CustomValidator
才会触发。如果您需要确保它不为空,那么您需要
RequiredFieldValidator
也是如此。编辑:
如果您的
CustomValidator
指定ControlToValidate
属性(并且您的原始示例也是如此),那么您的验证函数将仅当控件不为空时被调用。如果您没有指定
ControlToValidate
,那么每次都会调用您的验证函数。这为问题提供了第二种可能的解决方案。 您可以从
CustomValidator
中省略ControlToValidate
属性,并设置验证函数来执行如下操作,而不是使用单独的RequiredFieldValidator
:代码 (Javascript):
服务器端代码 (C#):
Your
CustomValidator
will only fire when theTextBox
isn't empty.If you need to ensure that it's not empty then you'll need a
RequiredFieldValidator
too.EDIT:
If your
CustomValidator
specifies theControlToValidate
attribute (and your original example does) then your validation functions will only be called when the control isn't empty.If you don't specify
ControlToValidate
then your validation functions will be called every time.This opens up a second possible solution to the problem. Rather than using a separate
RequiredFieldValidator
, you could omit theControlToValidate
attribute from theCustomValidator
and setup your validation functions to do something like this:Client Side code (Javascript):
Server side code (C#):
客户端验证根本没有在我的网络表单上执行,我不知道为什么。 事实证明,问题是 JavaScript 函数的名称与服务器控件 ID 相同。
所以你不能这样做......
但这可行:
我猜它与内部 .NET Javascript 冲突?
Client-side validation was not being executed at all on my web form and I had no idea why. It turns out the problem was the name of the javascript function was the same as the server control ID.
So you can't do this...
But this works:
I'm guessing it conflicts with internal .NET Javascript?
另请检查您是否未使用验证组,因为如果设置了验证组属性且未通过显式调用,则验证不会触发
Also check that you are not using validation groups as that validation wouldnt fire if the validationgroup property was set and not explicitly called via
您是否验证导致回发的控件已将 CausesValidation 设置为 tru 并且没有为其分配验证组?
我不确定还有什么可能导致这种行为。
Did you verify that the control causing the post back has CausesValidation set to tru and that it does not have a validation group assigned to it?
I'm not sure what else might cause this behavior.
如果客户端验证无效,则不会触发服务器端验证,不会发送回发。
难道你还有其他未通过的验证吗?
客户端验证未执行,因为您指定了
ClientValidationFunction="TextBoxDTownCityClient"
并且这将查找名为TextBoxDTownCityClient
的函数作为验证函数,但函数名称应为TextBoxDAddress1Client
(如您所写)
Server-side validation won't fire if client-side validation is invalid, the postback is not send.
Don't you have some other validation that doesn't pass?
The client-side validation is not executed because you specified
ClientValidationFunction="TextBoxDTownCityClient"
and this will look for a function namedTextBoxDTownCityClient
as validation function, but the function name should beTextBoxDAddress1Client
(as you wrote)
感谢您提供有关 ControlToValidate LukeH 的信息!
我在代码中试图做的只是确保当文本字段 B 具有特定值时,某些文本字段 A 在字段中包含一些文本。 否则,A 可以为空或其他任何内容。 摆脱标记中的 ControlToValidate="A" 解决了我的问题。
干杯!
Thanks for that info on the ControlToValidate LukeH!
What I was trying to do in my code was to only ensure that some text field A has some text in the field when text field B has a particular value. Otherwise, A can be blank or whatever else. Getting rid of the ControlToValidate="A" in my mark up fixed the issue for me.
Cheers!