Building a rendered control with child controls


T

TS

I am building a control that inherits from Textbox. I am adding a required
field validator to the controls. should i be inheriting from WebControl
instead and overriding CreateChildControls?

Why or why not?

thanks
 
Ad

Advertisements

T

TS

Oh yeah, and those controls are added during load and they are rendered in
Render (actually this isnt my code). I guess they are building a composite
control as a rendered control.

what benefits do you get when you override a control (rendered control)
versus a composite control (say you had only a single control you wanted to
display in a custom control, would benefits do you get when you make it a
rendered control versus a composite control?)

thanks again!
 
W

Walter Wang [MSFT]

Hi TS,

I think a TextBox with validation is a special case here since you could
either use a composite control or implements IValidator interface to add
validation function to a class
(http://www.codeproject.com/aspnet/selfvalidatingtextbox.asp).

For a composite control with a single constituent control, the difference
between composition and inheritance would be:

1) A composite control will need to implement INamingContainer, which will
create a naming container for the children.

2) Inherited control still has public methods/properties for your user,
while a composite control will have to expose the only child to let your
user use the properties/methods directly.

Other than these, I believe other things such as performance will not be
affected.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

TS

Some of these controls i see like i said just render themselves and any
other controls in their controls collection. They add their validation
controls as controls of the control and then render them separately. Using
this scenario for DropdownList i have a problem that errors out: "does not
allow child controls". the control is inherited from dropdownlist and so it
won't work like the textbox control.

How can i get it to work while still keeping it as a rendered (inherited)
control?

since i get this "does not allow child controls", I've tried to add the
validation control to the control's parent.controls and the control's
page.controls collection to get around it but i then get "The control
collection cannot be modified during DataBind, Init, Load, PreRender or
Unload phases", so I am kinda stuck.

If i just simply render the control instead of add it to the control
hierarchy, it won't validate correctly though it is in the html source.

thanks
 
T

TS

note that this validator is not getting added to the javascript variable
Page_Validators
 
Ad

Advertisements

W

Walter Wang [MSFT]

Hi TS,

If you use the "inherited control" approach, you cannot use existing
validator controls, you have to implement IValiator interface yourself, as
http://www.codeproject.com/aspnet/selfvalidatingtextbox.asp described.

If you want to use the existing validator controls, you will have to use
the "composite control" approach.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

TS

OK, i can do the IValidator and i add it to Page.Validators on OnInit, but
no validation control are added to the page hierarchy and Page_validators in
js doesn't contain it. The project framework i'm using relies on the
validation control to be in this Page_Validators array for custom client
side validation.

Also, i guess i would have to manually code all the things the validation
controls do by default? How would i handle .Display =
ValidatorDisplay.Dynamic and .ValidationGroup = "xxxx"

While working on the dropdownlist i noticed i couldn't change (add controls
to) the control hierarchy. i'm wondering if there is any time in the page's
lifecycle that i could add controls to the control hierarchy - maybe check
if any controls implement IValidator and if so then create regular validator
and add to hierarchy.

thanks
 
T

TS

also if i kept the IValidator interface, i noticed i had to call .Validate()
myself - i figured the .net framework would call this automatically, but it
didn't.

thanks again walter
 
T

TS

ok, i guess i'm going to have to either have my control implementing
IValidator create a real validator so it goes into Page_Validators, or
abandon this and put as a separate control on page. Any ideas
 
W

Walter Wang [MSFT]

Hi TS,

Yes you will have to implement various functionality yourself, such as
client-side validation, this is because IValidator is only a interface. The
client-side validation behavior you're seeing for other built-in validators
are all inherited from BaseValidator, which implements IValidator interface
but adds rich functionality which is not required by IValidator.

I understand you actually want to use existing validators in your control,
this is also recommended way since it will let you use existing validators.
In this case, I think using a composite control is better.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Ad

Advertisements

W

Walter Wang [MSFT]

Hi TS,

Let me know if there's anything else I can help.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

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

Top