validation control unable to find its control to validate

Posted by nat on Stack Overflow See other posts from Stack Overflow or by nat
Published on 2010-04-29T12:36:15Z Indexed on 2010/04/29 12:47 UTC
Read the original article Hit count: 356

Filed under:
|
|
|

i have a repeater that is bound to a number of custom dataitems/types

on the itemdatabound event for the repeater the code calls a renderedit function that depending on the custom datatype will render a custom control. it will also (if validation flag is set) render a validation control for the appropriate rendered edit control

the edit control overrides the CreateChildControls() method for the custom control adding a number of literalControls thus

        protected override void CreateChildControls()
    {
        //other bits removed - but it is this 'hidden' control i am trying to validate
        this.Controls.Add(new LiteralControl(string.Format(
                                             "<input type=\"text\" name=\"{0}\" id=\"{0}\" value=\"{1}\" style=\"display:none;\" \">"
                                             , this.UniqueID
                                             , this.MediaId.ToString())
                                             ));
        //some other bits removed
    }

the validation control is rendered like this: where the passed in editcontrol is the control instance of which the above createchildcontrols is a method of..

    public override Control RenderValidationControl(Control editControl)
    {
        Control ctrl = new PlaceHolder();

        RequiredFieldValidator req = new RequiredFieldValidator();
        req.ID = editControl.ClientID + "_validator";
        req.ControlToValidate = editControl.UniqueID;
        req.Display = ValidatorDisplay.Dynamic;
        req.InitialValue = "0";
        req.ErrorMessage = this.Caption + " cannot be blank";
        ctrl.Controls.Add(req);

        return ctrl;
    }

the problem is, altho the validation controls .ControlToValidate property is set to the uniqueid of the editcontrol. when i hit the page i get the following error: Unable to find control id 'FieldRepeater$ctl01$ctl00' referenced by the 'ControlToValidate' property of 'FieldRepeater_ctl01_ctl00_validator'.

i have tried changing the literal in the createchildcontrols to a new TextBox(), and then set the id etc then, but i get a similar problem.

can anyone enlighten me? is this because of the order the controls are rendered in? ie the validation control is written before the editcontrol? or...

anyhow any help much appreciated

thanks

nat

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about validation