views:

854

answers:

2

Hi All,

I have created a user control (from System.Web.UI.UserControl), and created my own validator for the user control (from System.Web.UI.WebControls.BaseValidator). Everything works ok until I try to get the user control to do client side validation.

While trying to debug this issue I have set 'Control to Validate' to a text box instead of the custom user control, and the client side script works fine! It appears to me that it has an a issue with my composite user control I have created. Has anyone encountered this issue before? Has anyone else seen client side validation fail on custom user controls?

Some extra info :

The composite control is a drop down list and 'loader image', as it is a ajax enabled drop down list (using ICallbackEventHandler). I know that the client side javascript is being written to the page, and have placed an alert('random message') as the first line in the validator function that only appears if it is validating a text box (i.e. not when it is validating my custom control)

Language : C# (ASP.NET 2.0) and jQuery 1.2.6

in aspx file :

<rms:UserDDL ID="ddlUserTypes" runat="server" PreLoad="true" />
<rms:DDLValidator 
        ID="userTypesVal"
        ControlToValidate="ddlUserTypes"
        ErrorMessage="You have not selected a UserType"
        runat="server"
        Text="You have not selected a UserType"
        Display="Dynamic"
        EnableClientScript="true" />

in validator code behind

protected string ScriptBlock
        {
            get
            {
                string nl = System.Environment.NewLine;
                return
                    "<script type=\"text/javascript\">" + nl +
                    "   function " + ScriptBlockFunctionName + "(ctrl)" + nl +
                    "   {" + nl +
                    "       alert('Random message'); " + nl +
                    "       var selVal = $('#' + ctrl.controltovalidate).val(); " + nl +

                    "       alert(selVal);" + nl +
                    "      if (selVal === '-1') return false;  " + nl +
                    "      return false;  " + nl +
                    "   }" + nl +
                    "</script>";
            }
        }

    protected override void OnPreRender(EventArgs e)
    {
        if (this.DetermineRenderUplevel() && this.EnableClientScript)
        {
            Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", this.ScriptBlockFunctionName);
            Page.ClientScript.RegisterClientScriptBlock(GetType(), this.ScriptBlockKey, this.ScriptBlock);
        }

        base.OnPreRender(e);
    }

I know my ControlPropertiesValid() and EvaluateIsValid() work ok.

I appreciate any help on this issue.

Noel.

A: 

Try using a CustomValidator instead of a BaseValidator:

http://msdn.microsoft.com/en-us/library/9eee01cx(VS.71).aspx

And set the ClientValidationFunction to ensure your JS is called:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.clientvalidationfunction.aspx

Chris
I may have to resort to the CustomValidator to get javascript working, but that is not as reusable as extending BaseValidator. :(
It should be - the custom validator extends the base validator, so you should not lose any functionality there. Was there a specific area you were worried about?
Chris
well the custom validator (v) would require functionality to be added to it in order to work, so if I extend the base [v], I write the functionality once and then just include everywhere I need it. The custom [v] would need to be placed in a user control to be as reusable, which is extra headache.
Ahh - you could just extend the custom validator - that will make it re-usable for you, encapsulate your logic, etc whilst also allowing you to take advantage of the existing functionality to trigger a validator on the client via script?
Chris
A: 

Have you figured this out?