简体   繁体   English

客户端自定义验证器中的服务器端变量

[英]Server side variable in client side custom validator

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). 当服务器端列表不为空(或: ListCount变量> 0)时,它必须有效。 This list may change after the page has been loaded (via buttons on update panel): 加载页面后,此列表可能会更改(通过更新面板上的按钮):

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获取一些服务器端变量的当前值?

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函数并检查它是否包含option html元素,这应该可以解决问题。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM