I have a custom validator on a page:
我在页面上有一个自定义验证器:
<asp:CustomValidator ID="CustomValidator2" runat="server"
ControlToValidate="ddlProposer" ErrorMessage="Please select some values."
Display="Dynamic" onservervalidate="CustomValidator2_ServerValidate"
ClientValidationFunction="CustomValidator2_ClientValidate">
</asp:CustomValidator>
It must be valid, when a server-side list is not empty (or: the ListCount
variable > 0). This list may change after the page has been loaded (via buttons on update panel):
当服务器端列表不为空(或:ListCount变量> 0)时,它必须有效。加载页面后,此列表可能会更改(通过更新面板上的按钮):
public partial class Pages_Application_Application : System.Web.UI.Page
{
protected List<IdValue> ProposersList
{
get
{
if (ViewState["proposersList"] == null)
ViewState["proposersList"] = new List<IdValue>();
return ViewState["proposersList"] as List<IdValue>;
}
set
{
ViewState["proposersList"] = value;
}
}
public int ListCount
{
get
{
return this.ProposersList.Count;
}
}
...
There is no problem with server-side validation:
服务器端验证没有问题:
protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = this.ProposersList.Count > 0;
}
The problem is with client-side part. I've been trying something like this:
问题出在客户端部分。我一直在尝试这样的事情:
<script type="text/javascript">
function CustomValidator2_ClientValidate(source, arguments) {
var serverVariable = <%= ListCount %>;
alert(serverVariable);
arguments.IsValid = serverVariable > 0;
}
</script>
however, it fires only on first page load, and the ListCount variable is always 0 (so does the serverVariable).
但是,它仅在第一页加载时触发,而ListCount变量始终为0(serverVariable也是如此)。
The question is: is there an easy-way to make it working? So the Javascript gets the current value of some server-side variable?
问题是:是否有一种简单的方法使其有效?那么Javascript获取一些服务器端变量的当前值?
2 个解决方案
#1
1
you can use hidden variable on the page level and by setting its value from server side and validate on client side.
您可以在页面级别使用隐藏变量,并通过从服务器端设置其值并在客户端进行验证。
<input type="hidden" id="ListCount" runat="server" value="0" />
public partial class Pages_Application_Application : System.Web.UI.Page
{
protected List<IdValue> ProposersList
{
get
{
if (ViewState["proposersList"] == null)
ViewState["proposersList"] = new List<IdValue>();
return ViewState["proposersList"] as List<IdValue>;
}
set
{
ViewState["proposersList"] = value;
ListCount=value;
}
}
public int ListCount
{
get
{
return this.ProposersList.Count;
}
}
<script type="text/javascript">
function CustomValidator2_ClientValidate(source, arguments) {
var count= document.getElementById("ListCount").value;
alert(count);
arguments.IsValid = count > 0;
}
#2
1
You'll have to do it in plain javascript, and there is no sens of getting the server side variable since it won't be up to date at the moment client validation will be done.
你必须在普通的javascript中完成它,并且没有获得服务器端变量的感觉,因为它将在客户端验证完成时不是最新的。
What you need is pass your ddl html element to your CustomValidator2_ClientValidate
function and check if it contains option
html elements, that should do the trick.
你需要的是将你的ddl html元素传递给你的CustomValidator2_ClientValidate函数并检查它是否包含选项html元素,这应该可以解决问题。
#1
1
you can use hidden variable on the page level and by setting its value from server side and validate on client side.
您可以在页面级别使用隐藏变量,并通过从服务器端设置其值并在客户端进行验证。
<input type="hidden" id="ListCount" runat="server" value="0" />
public partial class Pages_Application_Application : System.Web.UI.Page
{
protected List<IdValue> ProposersList
{
get
{
if (ViewState["proposersList"] == null)
ViewState["proposersList"] = new List<IdValue>();
return ViewState["proposersList"] as List<IdValue>;
}
set
{
ViewState["proposersList"] = value;
ListCount=value;
}
}
public int ListCount
{
get
{
return this.ProposersList.Count;
}
}
<script type="text/javascript">
function CustomValidator2_ClientValidate(source, arguments) {
var count= document.getElementById("ListCount").value;
alert(count);
arguments.IsValid = count > 0;
}
#2
1
You'll have to do it in plain javascript, and there is no sens of getting the server side variable since it won't be up to date at the moment client validation will be done.
你必须在普通的javascript中完成它,并且没有获得服务器端变量的感觉,因为它将在客户端验证完成时不是最新的。
What you need is pass your ddl html element to your CustomValidator2_ClientValidate
function and check if it contains option
html elements, that should do the trick.
你需要的是将你的ddl html元素传递给你的CustomValidator2_ClientValidate函数并检查它是否包含选项html元素,这应该可以解决问题。