SJMan
SJMan

Reputation: 1607

How to set custom validator isValid property on Client Side?

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.

Below is some code : Code Behind :

protected void Page_Load(object sender, EventArgs e)
{
    if (hiddenfieldValue == true)
    {
        btnSend.OnClick = "someJavascriptFunction()";
    }
}

ASPX File :

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.

Upvotes: 2

Views: 20684

Answers (2)

Crab Bucket
Crab Bucket

Reputation: 6277

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

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

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

Basic example 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

ALSO

If you are using validation groups which you probably will be the call will become

Page_ClientValidate(validationGroupName);

Method 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

isvalid Boolean property.This is a property on each client validator indicating whether it is currently valid

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

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

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.

 btnSend.OnClientClick = "someJavascriptFunction()";

Upvotes: 3

Raghubar
Raghubar

Reputation: 2788

Use This to Bind the Event.

btnSend.Attributes.Add("onclick","return someJavascriptFunction();")

Upvotes: 0

Related Questions