[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

TS

11/20/2007 3:34:00 PM

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


4 Answers

wawang

11/21/2007 2:40:00 AM

0

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/aa4...


> 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/textboxwithval...

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@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.

TS

11/26/2007 4:16:00 PM

0

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@online.microsoft.com> wrote in message
news:Xk8Llf%23KIHA.5204@TK2MSFTNGHUB02.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/aa4...
>
>
>> 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/textboxwithval...
>
> 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@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.
>


TS

11/26/2007 4:34:00 PM

0

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@nospam.nospam> wrote in message
news:u$rPyeEMIHA.4808@TK2MSFTNGP05.phx.gbl...
>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@online.microsoft.com> wrote in message
> news:Xk8Llf%23KIHA.5204@TK2MSFTNGHUB02.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/aa4...
>>
>>
>>> 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/textboxwithval...
>>
>> 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@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.
>>
>
>


wawang

11/27/2007 11:02:00 AM

0

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@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.