开发者

validation control unable to find its control to validate

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 cus开发者_开发百科tom 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜