I am having a problem setting the customvalidator on client side. based on a particular hidden field value, the onClientClick event of a button should be firing a function which sets the isValid property of a CustomValidator to false.
在客户端设置customvalidator时,我遇到了一个问题。基于一个特定的隐藏字段值,一个按钮的onClientClick事件应该触发一个函数,该函数设置一个CustomValidator的isValid属性为false。
Below is some code : Code Behind :
下面是一些代码:代码:
protected void Page_Load(object sender, EventArgs e)
{
if (hiddenfieldValue == true)
{
btnSend.OnClick = "someJavascriptFunction()";
}
}
ASPX File :
ASPX文件:
function someJavascriptFunction()
{
if (hiddenfieldValue == true)
// Show Validation and dont do postback
vldValdiator.isValid = false;
return false; //Dont do Postback
else
return true; Do Postback
}
<asp:CustomValidator ID="vldValidator" runat="server" Text = "ABC"/>
I am not able to set the isValid property on client side based hidden field values. Please Help . Thanks in advance.
我不能在基于客户端的隐藏字段值中设置isValid属性。请帮助。提前谢谢。
2 个解决方案
#1
3
Method 1
方法1
You could try setting the Page_IsValid
property to false within the button onClientClick event. This would force the page into it's non validate state as far as the asp.net validators are concerned.
您可以尝试在clientclick事件的按钮中设置Page_IsValid属性为false。这将迫使页面进入它的非验证状态,就像asp.net验证器所关心的那样。
Basic example
基本的例子
function ButtonOnClickClient()
{
if ($('#hiddenFieldID').val() == 'someValue')
{
Page_IsValid = false;
}
}
Honestly - i'm not convinced that the Page_IsValid property won't be reset. You would need to try
老实说,我不相信Page_IsValid属性不会被重置。你需要试一试。
Method 2
方法2
you could make the customvalidator client validation check the hidden field and make the validator pass or fail validation on that basis. The button onclient click event could then call the Page_ClientValidate()
function which will force validation on the page. This will then set the correct validation property.
您可以使customvalidator客户端验证检查隐藏字段,并在此基础上使验证器通过或失败验证。客户端单击事件的按钮可以调用Page_ClientValidate()函数,该函数将在页面上强制验证。这将设置正确的验证属性。
Basic example 2
基本示例2
function CustomValidatorClientValidate(source, arguments)
{
if ($('#hiddenFieldID').val() == 'someValue'){
arguments.IsValid = true;
} else {
arguments.IsValid = false;
}
}
function ButtonOnClientClick()
{
Page_ClientValidate();
}
This uses JQuery - because i find it easier to write an example that way. By no means mandatory to use it. Also I'm assuming the asp.net validator API is present. It should be if you have included those controls but you could always tighten up by adding checks for the function names
这使用了JQuery——因为我发现以这种方式编写示例更容易。绝不要强制使用它。我还假设asp.net validator API存在。如果您已经包含了这些控件,那么它应该是这样的,但是您可以通过为函数名添加检查来加强。
ALSO
也
If you are using validation groups which you probably will be the call will become
如果您使用的是验证组,您可能会调用它。
Page_ClientValidate(validationGroupName);
Method 3
方法3
You could try that about but set the the isvalid property of the customvalidator. That should give you the more direct control of the validator that you want. The reference below says
您可以尝试这样做,但是设置了customvalidator的isvalid属性。这将使您能够更直接地控制您想要的验证器。参考下面的说
isvalid Boolean property.This is a property on each client validator indicating whether it is currently valid
isvalid布尔属性。这是每个客户机验证器上的一个属性,它指示它当前是否有效。
I've done something similar to the first two but I've not personally tried this one.
我做过类似的事情,但我没有亲自尝试过。
In general
在一般情况下
The general the method you are using is direct manipulation of the javascript API that the asp.net validators use. It's not massively pretty but you can get the control that you want
通常使用的方法是对asp.net验证器使用的javascript API进行直接操作。它不是很漂亮,但是你可以得到你想要的控制。
Reference
参考
This gives a reference to the javascript API you are trying to use. It's a lengthy article but the bit you want is about halfway down
这将为您尝试使用的javascript API提供参考。这是一篇很长的文章,但是你想要的那部分是在半路上。
http://msdn.microsoft.com/en-us/library/Aa479045
http://msdn.microsoft.com/en-us/library/Aa479045
Good Luck
祝你好运
More
更多的
I really need to leave this but your code is wrong here - it's OnClientClick.
我真的需要离开这里,但是你的代码是错误的-它是OnClientClick。
btnSend.OnClientClick = "someJavascriptFunction()";
#2
0
Use This to Bind the Event.
用这个来绑定事件。
btnSend.Attributes.Add("onclick","return someJavascriptFunction();")
btnSend.Attributes。添加(“onclick”、“返回someJavascriptFunction();")
#1
3
Method 1
方法1
You could try setting the Page_IsValid
property to false within the button onClientClick event. This would force the page into it's non validate state as far as the asp.net validators are concerned.
您可以尝试在clientclick事件的按钮中设置Page_IsValid属性为false。这将迫使页面进入它的非验证状态,就像asp.net验证器所关心的那样。
Basic example
基本的例子
function ButtonOnClickClient()
{
if ($('#hiddenFieldID').val() == 'someValue')
{
Page_IsValid = false;
}
}
Honestly - i'm not convinced that the Page_IsValid property won't be reset. You would need to try
老实说,我不相信Page_IsValid属性不会被重置。你需要试一试。
Method 2
方法2
you could make the customvalidator client validation check the hidden field and make the validator pass or fail validation on that basis. The button onclient click event could then call the Page_ClientValidate()
function which will force validation on the page. This will then set the correct validation property.
您可以使customvalidator客户端验证检查隐藏字段,并在此基础上使验证器通过或失败验证。客户端单击事件的按钮可以调用Page_ClientValidate()函数,该函数将在页面上强制验证。这将设置正确的验证属性。
Basic example 2
基本示例2
function CustomValidatorClientValidate(source, arguments)
{
if ($('#hiddenFieldID').val() == 'someValue'){
arguments.IsValid = true;
} else {
arguments.IsValid = false;
}
}
function ButtonOnClientClick()
{
Page_ClientValidate();
}
This uses JQuery - because i find it easier to write an example that way. By no means mandatory to use it. Also I'm assuming the asp.net validator API is present. It should be if you have included those controls but you could always tighten up by adding checks for the function names
这使用了JQuery——因为我发现以这种方式编写示例更容易。绝不要强制使用它。我还假设asp.net validator API存在。如果您已经包含了这些控件,那么它应该是这样的,但是您可以通过为函数名添加检查来加强。
ALSO
也
If you are using validation groups which you probably will be the call will become
如果您使用的是验证组,您可能会调用它。
Page_ClientValidate(validationGroupName);
Method 3
方法3
You could try that about but set the the isvalid property of the customvalidator. That should give you the more direct control of the validator that you want. The reference below says
您可以尝试这样做,但是设置了customvalidator的isvalid属性。这将使您能够更直接地控制您想要的验证器。参考下面的说
isvalid Boolean property.This is a property on each client validator indicating whether it is currently valid
isvalid布尔属性。这是每个客户机验证器上的一个属性,它指示它当前是否有效。
I've done something similar to the first two but I've not personally tried this one.
我做过类似的事情,但我没有亲自尝试过。
In general
在一般情况下
The general the method you are using is direct manipulation of the javascript API that the asp.net validators use. It's not massively pretty but you can get the control that you want
通常使用的方法是对asp.net验证器使用的javascript API进行直接操作。它不是很漂亮,但是你可以得到你想要的控制。
Reference
参考
This gives a reference to the javascript API you are trying to use. It's a lengthy article but the bit you want is about halfway down
这将为您尝试使用的javascript API提供参考。这是一篇很长的文章,但是你想要的那部分是在半路上。
http://msdn.microsoft.com/en-us/library/Aa479045
http://msdn.microsoft.com/en-us/library/Aa479045
Good Luck
祝你好运
More
更多的
I really need to leave this but your code is wrong here - it's OnClientClick.
我真的需要离开这里,但是你的代码是错误的-它是OnClientClick。
btnSend.OnClientClick = "someJavascriptFunction()";
#2
0
Use This to Bind the Event.
用这个来绑定事件。
btnSend.Attributes.Add("onclick","return someJavascriptFunction();")
btnSend.Attributes。添加(“onclick”、“返回someJavascriptFunction();")