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!
|