Question

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.

No correct solution

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top