HighTechTalks DotNet Forums  

control rendering question

ASP.net Building Controls microsoft.public.dotnet.framework.aspnet.buildingcontrols


Discuss control rendering question in the ASP.net Building Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
TS
 
Posts: n/a

Default control rendering question - 11-20-2007 , 10:34 AM






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



Reply With Quote
  #2  
Old   
Walter Wang [MSFT]
 
Posts: n/a

Default RE: control rendering question - 11-20-2007 , 09:40 PM






Hi TS,

Quote:
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


Quote:
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)

Quote:
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/te...hvalidator.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 (wawang (AT) online (DOT) 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.



Reply With Quote
  #3  
Old   
TS
 
Posts: n/a

Default Re: control rendering question - 11-26-2007 , 11:16 AM



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]"" <wawang (AT) online (DOT) microsoft.com> wrote

Quote:
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/te...hvalidator.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 (wawang (AT) online (DOT) 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.




Reply With Quote
  #4  
Old   
TS
 
Posts: n/a

Default Re: control rendering question - 11-26-2007 , 11:34 AM



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" <manofsteele1 (AT) nospam (DOT) nospam> wrote

Quote:
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]"" <wawang (AT) online (DOT) microsoft.com> wrote in message
news:Xk8Llf%23KIHA.5204 (AT) TK2MSFTNGHUB02 (DOT) phx.gbl...
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/te...hvalidator.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 (wawang (AT) online (DOT) 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.






Reply With Quote
  #5  
Old   
Walter Wang [MSFT]
 
Posts: n/a

Default Re: control rendering question - 11-27-2007 , 06:01 AM



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 (wawang (AT) online (DOT) 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.


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.