User created Validator wont call Client side validation Javascript on 'complex' user control.

Posted on Stack Overflow See other posts from Stack Overflow
Published on 2009-02-25T14:42:20Z Indexed on 2010/05/28 14:02 UTC
Read the original article Hit count: 296

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.

© Stack Overflow or respective owner

Related posts about asp.net-2.0

Related posts about client-side