locked
Custom User Control Validation RRS feed

  • Question

  • User-246671484 posted

    Hi Folks,

    I have built a fairly simple User Control that contains a drop-down list and a textbox. I included some JavaScript that displays the textbox if "other" is selected in the drop-down list and hides the textbox if not. The Control has a property named "Value" that reads the text in the drop-down unless "other" is selected in which case it reads from the textbox. And the set operation of that property looks up the text passed in the value parameter in the drop-down list and auto-selects it, unless it can't be found in which case it selects "other" and populates the textbox.

    This user control works like a charm.

    I then created a custom validation control that inherits from BaseValidator and is used to validate that an item is selected in the drop-down list, and if that item is 'other' then to ensure that the textbox has a value typed in.

    However, it doesn't want to validate on the client side. It validates on the server-side just fine.

    At first I thought the javascript wasn't being written to the client. But I created a button and used it to test and the javascript used to validate works.

    For some reason the client-side evaluationfunction event is not being called for this control when the submit button is clicked. All other validation controls do their job (client side).

    Here's the code in my validation control that I've used before...

            protected override void AddAttributesToRender(HtmlTextWriter writer)
            {

                base.AddAttributesToRender(writer);

                if (this.RenderUplevel)
                {

                    ScriptManager sm = ScriptManager.GetCurrent(this.Page);
                    if (sm != null)
                    {
                        // For Ajax enabled pages with ScriptManager
                        ScriptManager.RegisterExpandoAttribute(this, ClientID, "evaluationfunction", "ValidateSecurityQuestionRequired", false);
                    }
                    else
                    {
                        this.Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "ValidateSecurityQuestionRequired", false);
                    }
                }
            }

     

    Note also that I have decorated the user control with this...

    [ValidationProperty("Value")]

    "Value" being the property I spoke of above that gets or sets the value of the control.

     

    Anyone have any ideas? Has anyone else run into this?

    Tuesday, March 31, 2009 11:11 PM

Answers

  • User-246671484 posted

    Found a work around...

    http://siderite.blogspot.com/2007/03/creating-web-user-control-in-net-20.html

    To summarize... Microsoft's client side validation code looks for HTML controls that have a value attribute and the ClientID of the ControlToValidate.

    Your User Control must render as an HTML control with that name.

    Since User Controls are composite, none of the controls rendered in HTML have the ClientID of the ControlToValidate. So no client side validation can occur.

    To fix the problem, within your User Control code wrap all the other controls in a div tag with the same ClientID...

    <div id='<%=ClientID %>'>

    ... controls here

    </div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 1, 2009 12:21 PM

All replies

  • User1059346350 posted

     A few ideas about getting it to run client side:

    1. Make sure your validationGroup is set correctly.
    2. Does the declaration of your customValidationControl include EnableClientScript="true" and ValidateEmptyText="true"?
    3. Is the javascript method not being run, or is it just not setting the validation corretcly?
    Wednesday, April 1, 2009 10:03 AM
  • User-246671484 posted

    Thanks very much for your reply and great ideas.

    Unfortunately, no joy.

    No controls are using a validationGroup.

    EnableClientScript is true.

    ValidateEmptyText is a property of the CustomControl not BaseControl so it does not exist in my control. I'd LOVE to see the actual source code of that method though, or the source code of the "RequiredFieldValidator" to see what it does to force the client-side code to trigger when the value is empty.

    My javascript function, for now, just shows an alert() popup to let me know if it's even being triggered. And it's not, unless I call it manually (which was a test I did to make sure the script was being registered with the page)

    The problem seems to point to the fact that the control I'm validating is a composite User Control.

    Is there some hookup I have to make in JavaScript to cause the control to validate?

    Wednesday, April 1, 2009 11:32 AM
  • User-246671484 posted

    Found a work around...

    http://siderite.blogspot.com/2007/03/creating-web-user-control-in-net-20.html

    To summarize... Microsoft's client side validation code looks for HTML controls that have a value attribute and the ClientID of the ControlToValidate.

    Your User Control must render as an HTML control with that name.

    Since User Controls are composite, none of the controls rendered in HTML have the ClientID of the ControlToValidate. So no client side validation can occur.

    To fix the problem, within your User Control code wrap all the other controls in a div tag with the same ClientID...

    <div id='<%=ClientID %>'>

    ... controls here

    </div>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 1, 2009 12:21 PM
  • User1059346350 posted

     Ah, good point. I actually accidentally got around that problem. For a different reason I overwrite the ClientID in my compositeControl to return the ClientID of the main control within it. For instance a control that includes a textBox and several validators returns the clientID of the textBox.

    Thursday, April 2, 2009 4:15 AM
  • User-1337876127 posted

     Ah, good point. I actually accidentally got around that problem. For a different reason I overwrite the ClientID in my compositeControl to return the ClientID of the main control within it. For instance a control that includes a textBox and several validators returns the clientID of the textBox.

    Hanan, I am also looking to overwrite the ClientID to return the main control.  Do you have sample code on how you did this?

    Thanks,
    Adiel

    Wednesday, June 3, 2009 9:39 AM
  • User1059346350 posted

    Absolutely,

    In my user control I override the ClientID property and instead return the ClientID of my textbox.

    public override string ClientID
    {
        get { return txtValue.ClientID; }
    }

    Thursday, June 4, 2009 5:01 AM
  • User-1337876127 posted

     Thank You

    Wednesday, June 10, 2009 11:33 AM