C# ASP.NET 自定义验证器客户端和服务器端验证未触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/701029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET Custom Validator Client side & Server Side validation not firing
提问by REA_ANDREW
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 正在识别客户端功能:
ASP.NET JavaScript output:
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";
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:
只是对最后一条评论的另一个更新:其中 TextBox 不能为空。我对此进行了测试,但事实并非如此。在空白页面上,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" />
采纳答案by LukeH
Your CustomValidator
will only fire when the TextBox
isn't empty.
你CustomValidator
只会在TextBox
不为空时触发。
If you need to ensure that it's not empty then you'll need a RequiredFieldValidator
too.
如果您需要确保它不是空的,那么您也需要一个RequiredFieldValidator
。
注意:如果输入控件为空,则不会调用验证函数,验证成功。使用 RequiredFieldValidator 控件要求用户在输入控件中输入数据。
EDIT:
编辑:
If your CustomValidator
specifies the ControlToValidate
attribute (and your original example does) then your validation functions will only be called when the control isn't empty.
如果您CustomValidator
指定了ControlToValidate
属性(并且您的原始示例确实如此),那么您的验证函数只会在控件不为空时调用。
If you don't specify ControlToValidate
then your validation functions will be called every time.
如果您未指定,ControlToValidate
则每次都会调用您的验证函数。
This opens up a second possible solution to the problem. Rather than using a separate RequiredFieldValidator
, you could omit the ControlToValidate
attribute from the CustomValidator
and setup your validation functions to do something like this:
这为该问题提供了第二种可能的解决方案。而不是使用单独的RequiredFieldValidator
,您可以ControlToValidate
从 中省略该属性CustomValidator
并设置您的验证函数来执行以下操作:
Client Side code (Javascript):
客户端代码(Javascript):
function TextBoxDCountyClient(sender, args) {
var v = document.getElementById('<%=TextBoxDTownCity.ClientID%>').value;
if (v == '') {
args.IsValid = false; // field is empty
}
else {
// do your other validation tests here...
}
}
Server side code (C#):
服务器端代码(C#):
protected void TextBoxDTownCity_Validate(
object source, ServerValidateEventArgs args)
{
string v = TextBoxDTownCity.Text;
if (v == string.Empty)
{
args.IsValid = false; // field is empty
}
else
{
// do your other validation tests here...
}
}
回答by Daniel
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?
您是否验证了导致回发的控件的 CausesValidation 设置为 tru 并且没有为其分配验证组?
I'm not sure what else might cause this behavior.
我不确定还有什么可能导致这种行为。
回答by Davide Vosti
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 named TextBoxDTownCityClient
as validation function, but the function name should be
TextBoxDAddress1Client
客户端验证未执行,因为您指定ClientValidationFunction="TextBoxDTownCityClient"
,这将查找名为TextBoxDTownCityClient
验证函数的函数,但函数名称应为
TextBoxDAddress1Client
(as you wrote)
(如你所写)
回答by Bill Gates
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.
客户端验证根本没有在我的 Web 表单上执行,我不知道为什么。事实证明,问题是 javascript 函数的名称与服务器控件 ID 相同。
So you can't do this...
所以你不能这样做...
<script>
function vld(sender, args) { args.IsValid = true; }
</script>
<asp:CustomValidator runat="server" id="vld" ClientValidationFunction="vld" />
But this works:
但这有效:
<script>
function validate_vld(sender, args) { args.IsValid = true; }
</script>
<asp:CustomValidator runat="server" id="vld" ClientValidationFunction="validate_vld" />
I'm guessing it conflicts with internal .NET Javascript?
我猜它与内部 .NET Javascript 冲突?
回答by Bill Gates
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
还要检查您是否没有使用验证组,因为如果验证组属性已设置且未通过显式调用,则验证不会触发
Page.Validate({Insert validation group name here});
回答by Carl Nielsen
Use this:
用这个:
<asp:CustomValidator runat="server" id="vld" ValidateEmptyText="true"/>
To validate an empty field.
验证空字段。
You don't need to add 2 validators !
您不需要添加 2 个验证器!
回答by doddy
Thanks for that info on the ControlToValidate LukeH!
感谢您提供有关 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.
我试图在我的代码中做的是仅确保当文本字段 B 具有特定值时,某些文本字段 A 在字段中具有某些文本。否则, A 可以为空或其他任何内容。在我的标记中去掉 ControlToValidate="A" 为我解决了这个问题。
Cheers!
干杯!