简体   繁体   中英

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). 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).

The question is: is there an easy-way to make it working? So the Javascript gets the current value of some server-side variable?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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