control rendering question

Discussion in 'ASP .Net Building Controls' started by TS, Nov 20, 2007.

  1. TS

    TS Guest

    there seems to be different scenarios when controls that are added to a
    control's hiearchy are rendered. If you just add them to the hierachy and
    don't override the control's render method i believe they will just get
    rendered.

    I have some scenarios where i have built/seen controls and they seem to
    render their children differently:

    1. A control inherited from webControl that overrides CreateChildControls to
    add all of its controls to hierarchy. No render methods overridden. The
    controls are rendered in the order i added them to control hierarchy

    2. A control inherited from WebControl. It has controls added to hierachy
    during load. It overrides Render and explicitely loops thru all its controls
    and renders them (ReqFieldValidators, etc)

    3. A control inherited from TextBox. It has controls added to hierachy
    during load. It overrides Render and explicitely loops thru all its controls
    and renders them (ReqFieldValidators, etc)

    in #2, if i override RenderChildren it isn't called
    in #2, if i override CreateChildControls, its called so i add a control
    there, but RenderChildren is still not called (is it because i'm overriding
    Render?).

    i'm just confused. For #1 it seems like rendering is handled for me
    automatically and in #2 & #3 i have to explicitely call render for all child
    controls.

    What is going on?

    thanks
     
    TS, Nov 20, 2007
    #1
    1. Advertising

  2. Hi TS,

    > 1. A control inherited from webControl that overrides CreateChildControls

    to
    add all of its controls to hierarchy. No render methods overridden. The
    controls are rendered in the order i added them to control hierarchy

    This is the recommended approach to create a composite control that
    consists of several child controls. Actually in ASP.NET 2.0 we have a
    dedicated parent class to let you inherit from: CompositeControl.

    #A Crash Course on ASP.NET Control Development: Building Composite Controls
    http://msdn2.microsoft.com/en-us/library/aa479016.aspx


    > 2. A control inherited from WebControl. It has controls added to hierachy

    during load. It overrides Render and explicitely loops thru all its
    controls
    and renders them (ReqFieldValidators, etc)

    > 3. A control inherited from TextBox. It has controls added to hierachy

    during load. It overrides Render and explicitely loops thru all its
    controls
    and renders them (ReqFieldValidators, etc)


    I believe you're referring to a similar approach as this article shows:

    #Building an ASP.NET custom web control: a textbox and validator in one -
    The Code Project - ASP.NET
    http://www.codeproject.com/aspnet/textboxwithvalidator.asp

    Please note the RenderChildren is called by Render by default in class
    Control. However, a control could choose to override the Render method and
    not call base.Render() at all. In this case, TextBox's Render is not
    calling RenderChildren.

    In summary, this approach is NOT recommended to create a composite control.
    Actually the code in the CodeProject article has a bug: OnInit isn't called
    during design-time and the RequiredFieldValidator instance is null in
    Render.

    Hope this helps.


    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], Nov 21, 2007
    #2
    1. Advertising

  3. TS

    TS Guest

    i have tested the theory that RenderChildren will be called by base's
    implementation of Render and it doesn't seem to work.

    What i want to do is have a normal textbox that adds a ReqFieldValidator.
    Though in display only mode i want the textbox to render as a label and
    don't have the ReqFieldValidator render at all. So I've included what i'm
    doing below. When i set a breakpoint at RenderChildren (during edit mode) it
    is not hit even though i step thru the code and it calls base.Render in my
    Render override. Is the reason for this because the TextBox is not supposed
    to be used as a container control and since it doesn't implement
    INamingContainer?

    thanks
    protected override void Render(System.Web.UI.HtmlTextWriter writer){

    if (ReadOnly){

    Label label = new Label();
    label.CssClass = CssClass;
    label.Text = Text.Replace("\n", "<BR/>");
    label.ID = this.ClientID;
    label.RenderControl(writer);
    Page.ClientScript.RegisterHiddenField(ID, Text);
    }
    else{

    base.Render(writer);

    }
    }

    protected override void CreateChildControls(){

    RequiredFieldValidator validator = new RequiredFieldValidator();
    validator.ID = String.Format("RFV{0}", ID);
    validator.Display = ValidatorDisplay.Dynamic;
    validator.ControlToValidate = ID.ToString();
    validator.Label = ValidatorLabel;
    validator.SetFocusOnError = SetFocusOnError;
    validator.ValidationGroup = ValidationGroup;
    Controls.Add(validator);
    base.CreateChildControls();
    }

    protected override void RenderChildren(HtmlTextWriter writer){

    base.RenderChildren(writer);
    }


    ""Walter Wang [MSFT]"" <> wrote in message
    news:Xk8Llf%...
    > Hi TS,
    >
    >> 1. A control inherited from webControl that overrides CreateChildControls

    > to
    > add all of its controls to hierarchy. No render methods overridden. The
    > controls are rendered in the order i added them to control hierarchy
    >
    > This is the recommended approach to create a composite control that
    > consists of several child controls. Actually in ASP.NET 2.0 we have a
    > dedicated parent class to let you inherit from: CompositeControl.
    >
    > #A Crash Course on ASP.NET Control Development: Building Composite
    > Controls
    > http://msdn2.microsoft.com/en-us/library/aa479016.aspx
    >
    >
    >> 2. A control inherited from WebControl. It has controls added to hierachy

    > during load. It overrides Render and explicitely loops thru all its
    > controls
    > and renders them (ReqFieldValidators, etc)
    >
    >> 3. A control inherited from TextBox. It has controls added to hierachy

    > during load. It overrides Render and explicitely loops thru all its
    > controls
    > and renders them (ReqFieldValidators, etc)
    >
    >
    > I believe you're referring to a similar approach as this article shows:
    >
    > #Building an ASP.NET custom web control: a textbox and validator in one -
    > The Code Project - ASP.NET
    > http://www.codeproject.com/aspnet/textboxwithvalidator.asp
    >
    > Please note the RenderChildren is called by Render by default in class
    > Control. However, a control could choose to override the Render method and
    > not call base.Render() at all. In this case, TextBox's Render is not
    > calling RenderChildren.
    >
    > In summary, this approach is NOT recommended to create a composite
    > control.
    > Actually the code in the CodeProject article has a bug: OnInit isn't
    > called
    > during design-time and the RequiredFieldValidator instance is null in
    > Render.
    >
    > Hope this helps.
    >
    >
    > 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.
    >
     
    TS, Nov 26, 2007
    #3
  4. TS

    TS Guest

    i just looked at the Textbox's Render method using .Net Reflector to
    disassemble it and it shows this below showing that textbox doesn't call
    RenderChildren probably because it is not supposed to be used as a composite
    control, do you agree?
    protected internal override void Render(HtmlTextWriter writer)
    {
    this.RenderBeginTag(writer);
    if (this.TextMode == TextBoxMode.MultiLine)
    {
    HttpUtility.HtmlEncode(this.Text, writer);
    }
    this.RenderEndTag(writer);
    }


    "TS" <> wrote in message
    news:u$...
    >i have tested the theory that RenderChildren will be called by base's
    >implementation of Render and it doesn't seem to work.
    >
    > What i want to do is have a normal textbox that adds a ReqFieldValidator.
    > Though in display only mode i want the textbox to render as a label and
    > don't have the ReqFieldValidator render at all. So I've included what i'm
    > doing below. When i set a breakpoint at RenderChildren (during edit mode)
    > it is not hit even though i step thru the code and it calls base.Render in
    > my Render override. Is the reason for this because the TextBox is not
    > supposed to be used as a container control and since it doesn't implement
    > INamingContainer?
    >
    > thanks
    > protected override void Render(System.Web.UI.HtmlTextWriter writer){
    >
    > if (ReadOnly){
    >
    > Label label = new Label();
    > label.CssClass = CssClass;
    > label.Text = Text.Replace("\n", "<BR/>");
    > label.ID = this.ClientID;
    > label.RenderControl(writer);
    > Page.ClientScript.RegisterHiddenField(ID, Text);
    > }
    > else{
    >
    > base.Render(writer);
    >
    > }
    > }
    >
    > protected override void CreateChildControls(){
    >
    > RequiredFieldValidator validator = new RequiredFieldValidator();
    > validator.ID = String.Format("RFV{0}", ID);
    > validator.Display = ValidatorDisplay.Dynamic;
    > validator.ControlToValidate = ID.ToString();
    > validator.Label = ValidatorLabel;
    > validator.SetFocusOnError = SetFocusOnError;
    > validator.ValidationGroup = ValidationGroup;
    > Controls.Add(validator);
    > base.CreateChildControls();
    > }
    >
    > protected override void RenderChildren(HtmlTextWriter writer){
    >
    > base.RenderChildren(writer);
    > }
    >
    >
    > ""Walter Wang [MSFT]"" <> wrote in message
    > news:Xk8Llf%...
    >> Hi TS,
    >>
    >>> 1. A control inherited from webControl that overrides
    >>> CreateChildControls

    >> to
    >> add all of its controls to hierarchy. No render methods overridden. The
    >> controls are rendered in the order i added them to control hierarchy
    >>
    >> This is the recommended approach to create a composite control that
    >> consists of several child controls. Actually in ASP.NET 2.0 we have a
    >> dedicated parent class to let you inherit from: CompositeControl.
    >>
    >> #A Crash Course on ASP.NET Control Development: Building Composite
    >> Controls
    >> http://msdn2.microsoft.com/en-us/library/aa479016.aspx
    >>
    >>
    >>> 2. A control inherited from WebControl. It has controls added to
    >>> hierachy

    >> during load. It overrides Render and explicitely loops thru all its
    >> controls
    >> and renders them (ReqFieldValidators, etc)
    >>
    >>> 3. A control inherited from TextBox. It has controls added to hierachy

    >> during load. It overrides Render and explicitely loops thru all its
    >> controls
    >> and renders them (ReqFieldValidators, etc)
    >>
    >>
    >> I believe you're referring to a similar approach as this article shows:
    >>
    >> #Building an ASP.NET custom web control: a textbox and validator in one -
    >> The Code Project - ASP.NET
    >> http://www.codeproject.com/aspnet/textboxwithvalidator.asp
    >>
    >> Please note the RenderChildren is called by Render by default in class
    >> Control. However, a control could choose to override the Render method
    >> and
    >> not call base.Render() at all. In this case, TextBox's Render is not
    >> calling RenderChildren.
    >>
    >> In summary, this approach is NOT recommended to create a composite
    >> control.
    >> Actually the code in the CodeProject article has a bug: OnInit isn't
    >> called
    >> during design-time and the RequiredFieldValidator instance is null in
    >> Render.
    >>
    >> Hope this helps.
    >>
    >>
    >> 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.
    >>

    >
    >
     
    TS, Nov 26, 2007
    #4
  5. Hi TS,

    That's right. The TextBox is not meant to be used as a composite control. I
    suggest you inherit from the CompositeControl, then add the TextBox and the
    validator control as child controls to the composite control.


    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], Nov 27, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. threecrans
    Replies:
    0
    Views:
    1,434
    threecrans
    Jul 3, 2003
  2. matthias s.
    Replies:
    3
    Views:
    545
    Karl Seguin
    Jun 17, 2005
  3. Peter Mount
    Replies:
    4
    Views:
    959
    Peter Mount
    Jan 31, 2006
  4. William French

    rendering derived control from composite control

    William French, Dec 2, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    219
    Wayne
    Dec 3, 2003
  5. Bob Jones

    Composite control - sub control as a property not rendering

    Bob Jones, Apr 6, 2007, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    248
    Bob Jones
    Apr 9, 2007
Loading...

Share This Page