all groups > asp.net building controls > april 2007 >
You're in the

asp.net building controls

group:

Building a rendered control with child controls


Building a rendered control with child controls TS
4/26/2007 11:37:30 AM
asp.net building controls: 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

Re: Building a rendered control with child controls TS
4/26/2007 11:53:51 AM
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!

[quoted text, click to view]

Re: Building a rendered control with child controls wawang NO[at]SPAM online.microsoft.com
4/27/2007 12:00:00 AM
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 (wawang@online.microsoft.com, 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.
Re: Building a rendered control with child controls TS
4/27/2007 10:15:09 AM
thanks walter

[quoted text, click to view]

Re: Building a rendered control with child controls TS
5/16/2007 3:56:33 PM
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


[quoted text, click to view]

Re: Building a rendered control with child controls TS
5/16/2007 3:59:18 PM
note that this validator is not getting added to the javascript variable
Page_Validators

[quoted text, click to view]

Re: Building a rendered control with child controls TS
5/17/2007 11:01:04 AM
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

[quoted text, click to view]

Re: Building a rendered control with child controls TS
5/17/2007 11:02:58 AM
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

[quoted text, click to view]

Re: Building a rendered control with child controls wawang NO[at]SPAM online.microsoft.com
5/17/2007 12:03:18 PM
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 (wawang@online.microsoft.com, 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.
Re: Building a rendered control with child controls TS
5/17/2007 6:02:51 PM
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

[quoted text, click to view]

Re: Building a rendered control with child controls wawang NO[at]SPAM online.microsoft.com
5/18/2007 12:00:00 AM
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 (wawang@online.microsoft.com, 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.
Re: Building a rendered control with child controls wawang NO[at]SPAM online.microsoft.com
5/23/2007 12:00:00 AM
Hi TS,

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


Regards,
Walter Wang (wawang@online.microsoft.com, 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.
AddThis Social Bookmark Button