Update Property that mapped to a child control's property ?

S

Samuel

Hi,

I am developing a composite control for my own use and I am not sure what
the best practice is with regards to updating child control's property from a
mapped property on the custom control.

Should I

1) update the child control's property in the Setter of the composite
control's property by first making sure the constituent control isNot nothing

or

2) update the child control's property in the overriden Render method of the
composite control? I am overriding the Render property to add some HTML
without using the literalcontrol, so I must override the Render method
regardless or where I update the child control's property

Thanks!
 
S

Steven Cheng[MSFT]

Hi Programmer,

Welcome to ASPNET newsgroup.
As for the custom control property which mapped to child controls' certain
property, basically we have two means to define it:

1. Directly access and modify it through the child control's instance
reference in the custom control property's getter and setter accessor.
However, since the child control instance is only available after the
custom control's control hierarchy is constructed correctly, we need to
make sure this through some methods like (EnsureChildControls()) before we
access the child control instance....

2. We can also just declare another internal member field which hold the
underlying value of the custom control's property. Thus, the getter and
settor just return or update the internal member field of the custom
control. And we only apply the field's value to child controls before page
render out (maybe in Prerender event....).
Also, we can directly use ViewState to hold the value instead of using a
member field....

Here is a simple test webcontrol which contains two properties, one
directly mapped to child control's instance property, another directly
associated with viewstate entry:

====================
namespace WebControlLib
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:VSControl runat=server></{0}:VSControl>")]
[Designer(typeof(VSControlDesigner),typeof(IDesigner))]
public class VSControl : WebControl, INamingContainer
{

private Label _lbl;
private Table _tb;


public VSControl()
{
SetDefaultValues();

}





[Bindable(true)]
[Category("Appearance")]
[DefaultValue("Default Text")]
[Localizable(true)]
public string Text
{
get
{
EnsureChildControls();
return _lbl.Text;
}

set
{
EnsureChildControls();
_lbl.Text = value;

SetLabelPosition();
}
}

[Bindable(true)]
[Category("Appearance")]
[DefaultValue(Position.One)]
public Position LabelPosition
{
get
{

Position pos = (Position)ViewState["lbl_pos"];

return pos;
}
set
{

ViewState["lbl_pos"] = value;

EnsureChildControls();
SetLabelPosition();
}
}






protected override void CreateChildControls()
{
Controls.Clear();


_tb = new Table();
_tb.ID = "tbPanels";
_tb.BorderWidth = 1;
_tb.BorderStyle = BorderStyle.Solid;
_tb.BorderColor = System.Drawing.Color.Black;


TableRow tr = new TableRow();
tr.Cells.Add(new TableCell());
tr.Cells.Add(new TableCell());
tr.Cells[0].Width = tr.Cells[1].Width = Unit.Percentage(50);

_tb.Rows.Add(tr);

tr = new TableRow();
tr.Cells.Add(new TableCell());
tr.Cells.Add(new TableCell());
tr.Cells[0].Width = tr.Cells[1].Width = Unit.Percentage(50);

_tb.Rows.Add(tr);

Controls.Add(_tb);

_lbl = new Label();
_lbl.ID = "lblMessage";
_lbl.Text = Text;
_lbl.BorderStyle = BorderStyle.Solid;
_lbl.BorderWidth = 1;
_lbl.BorderColor = System.Drawing.Color.Blue;


Label lbl = new Label();
lbl.Text = "Cell One";
_tb.Rows[0].Cells[1].Controls.Add(lbl);

lbl = new Label();
lbl.Text = "Cell Two";
_tb.Rows[0].Cells[0].Controls.Add(lbl);

lbl = new Label();
lbl.Text = "Cell Three";
_tb.Rows[1].Cells[0].Controls.Add(lbl);

lbl = new Label();
lbl.Text = "Cell Four";
_tb.Rows[1].Cells[1].Controls.Add(lbl);



SetLabelPosition();

}

private void SetDefaultValues()
{
_lbl = new Label();
_lbl.Text = "Hello";

LabelPosition = Position.One;
}

private void SetLabelPosition()
{
switch (LabelPosition)
{
case Position.One:
_tb.Rows[0].Cells[1].Controls.Add(_lbl);
break;
case Position.Two:
_tb.Rows[0].Cells[0].Controls.Add(_lbl);
break;
case Position.Three:
_tb.Rows[1].Cells[0].Controls.Add(_lbl);
break;
case Position.Four:
_tb.Rows[1].Cells[1].Controls.Add(_lbl);
break;
}


}


}


public enum Position
{
One,
Two,
Three,
Four
}



public class VSControlDesigner : ControlDesigner
{
public override string GetDesignTimeHtml()
{
string dhtml = @"
<div><font size='20' color='red'>VSControl
Design-Time...</font></div>
";

return dhtml;
}
}
}
==============================

BTW, there're also many former threads in the newsgroup discussing on
creating cutsom webcontrols and manipulating custom contro properties...
You can have a search on the web to find some other reference...

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| Thread-Topic: Update Property that mapped to a child control's property ?
| thread-index: AcYgAxPypmFpUg+2RHmxwOTsbsZKog==
| X-WBNR-Posting-Host: 64.180.227.167
| From: =?Utf-8?B?U2FtdWVs?= <[email protected]>
| Subject: Update Property that mapped to a child control's property ?
| Date: Mon, 23 Jan 2006 01:55:02 -0800
| Lines: 19
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.buildingcontrols:14322
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
|
| Hi,
|
| I am developing a composite control for my own use and I am not sure what
| the best practice is with regards to updating child control's property
from a
| mapped property on the custom control.
|
| Should I
|
| 1) update the child control's property in the Setter of the composite
| control's property by first making sure the constituent control isNot
nothing
|
| or
|
| 2) update the child control's property in the overriden Render method of
the
| composite control? I am overriding the Render property to add some HTML
| without using the literalcontrol, so I must override the Render method
| regardless or where I update the child control's property
|
| Thanks!
|
 
S

Samuel

======================================
And we only apply the field's value to child controls before page
render out (maybe in Prerender event....).
Also, we can directly use ViewState to hold the value instead of using a
member field....
======================================

This is what I am doing right now, thanks for your reply. When you say apply
the fields value to child controls before render, what's the advantage or
implication of doing so in the control's PreRender event as opposed to the
control's Render method? Or does it not matter?

Thanks
 
S

Steven Cheng[MSFT]

Thanks for your quick response.

As for PreRender event, it is the last event where we can put some code to
manipulate our control or child controls' states and which will be
persisted in ViewState..... After that the control begin rendering itself
(in Render stage...) and our modification on control won't be persisted
into viewstate though the output still reflect our change.....

Also, for developing composite server control, we can also directly apply
the change to child control instance when the setter of property is called.
But as I mentioend, we need to make sure the childcontrol collection is
created at that time....

BTW, I do know that some guys do not like put code in PreRender or Render
event and prefer encapsulate control constructing and style applying code
in CreateChildControls function.... Anyway, we can choose any approach as
long as we make sure all the things are setting correctly according to the
webserver control's serverside lifecycle...

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| Thread-Topic: Update Property that mapped to a child control's property ?
| thread-index: AcYgkz+2Ncw1xmJgSdO3EVC7CJr8lQ==
| X-WBNR-Posting-Host: 209.200.117.19
| From: =?Utf-8?B?U2FtdWVs?= <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: Update Property that mapped to a child control's property ?
| Date: Mon, 23 Jan 2006 19:07:02 -0800
| Lines: 13
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet.buildingcontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.buildingcontrols:14325
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingcontrols
|
| ======================================
| And we only apply the field's value to child controls before page
| render out (maybe in Prerender event....).
| Also, we can directly use ViewState to hold the value instead of using a
| member field....
| ======================================
|
| This is what I am doing right now, thanks for your reply. When you say
apply
| the fields value to child controls before render, what's the advantage or
| implication of doing so in the control's PreRender event as opposed to
the
| control's Render method? Or does it not matter?
|
| Thanks
|
 

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

Members online

No members online now.

Forum statistics

Threads
474,034
Messages
2,570,356
Members
47,002
Latest member
RobertoLip

Latest Threads

Top