views:

145

answers:

1

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

+1  A: 

You have to use editControl.ID not editControl.UniqueID

Also take into account that if editControl must be something that can be used with a required field vaidator. It doesn't make sense this validator for ALL input controls.

EDIT
Check this link. It may be helpful since it's exaclty what you need
http://support.microsoft.com/kb/310082

Claudio Redi
hi thanks for your replyyes i have anumber of ovverides for different custom data types for the validation. but i should use editControl.ID even thought the control is rendered with id and name = this.UniqueID ?
nat
so i cannot validate this at all? even if the editcontrol contains a standard Textbox?can i use a custom validator?thanks
nat
@nat: I edited my post. Check the added link
Claudio Redi
hi again thanks for that, adding the validationproperty did the trick
nat