adding validator controls dynamically to a page

S

sonic

I am trying to dynamically load a validator and must be missing
something elementary here.

I extended TextBox control to add some functionality to it. One new
feature it contains is IsRequired property. if true, the control will
render it self differently, and should add a RequiredFiledValidator
control to the page.

so in my controls render event, i am adding the following:

if ( this.IsRequired) {
...// handle visual stuff ( works fine )
RequiredFieldValidator v = new RequiredFieldValidator();
v.ControlToValidate = this.ID;
v.EnableClientScript = true;
v.ErrorMessage = "*";

this.Page.Controls.Add(v);
}

the following code will compile, and i will get a runtime error that
the validator control could not find the ControlToValidate target.
"Unable to find control id 'TransactionTypeList' referenced by the
'ControlToValidate' property of ''.
i thought, this is understandably so, since MyTextBox could be rendered
in another user control ( vs Page ).

so i tried modifying the last line to
this.NamingContainer.Controls.Add(v);
but i get another runtime error after that of
The Controls collection cannot be modified because the control contains
code blocks (i.e. <% ... %>).

am i going about this all wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top