Building a rendered control with child controls

Discussion in 'ASP .Net Building Controls' started by TS, Apr 26, 2007.

  1. TS

    TS Guest

    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
     
    TS, Apr 26, 2007
    #1
    1. Advertisements

  2. TS

    TS Guest

    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!
     
    TS, Apr 26, 2007
    #2
    1. Advertisements

  3. 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 (, 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.
     
    Walter Wang [MSFT], Apr 27, 2007
    #3
  4. TS

    TS Guest

    thanks walter
     
    TS, Apr 27, 2007
    #4
  5. TS

    TS Guest

    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
     
    TS, May 16, 2007
    #5
  6. TS

    TS Guest

    note that this validator is not getting added to the javascript variable
    Page_Validators
     
    TS, May 16, 2007
    #6
  7. 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 (, 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.
     
    Walter Wang [MSFT], May 17, 2007
    #7
  8. TS

    TS Guest

    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
     
    TS, May 17, 2007
    #8
  9. TS

    TS Guest

    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
     
    TS, May 17, 2007
    #9
  10. TS

    TS Guest

    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
     
    TS, May 18, 2007
    #10
  11. 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 (, 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.
     
    Walter Wang [MSFT], May 18, 2007
    #11
  12. Hi TS,

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


    Regards,
    Walter Wang (, 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.
     
    Walter Wang [MSFT], May 23, 2007
    #12
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.