control rendering question


T

TS

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
 
Ad

Advertisements

W

Walter Wang [MSFT]

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 ([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

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]" said:
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 ([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

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 said:
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]" said:
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 ([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,

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 ([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