Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Update Property that mapped to a child control's property ?

Reply
Thread Tools

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

 
 
Samuel
Guest
Posts: n/a
 
      01-23-2006
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!
 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      01-24-2006
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(IDesigne r))]
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?= <(E-Mail Removed)>
| Subject: Update Property that mapped to a child control's property ?
| Date: Mon, 23 Jan 2006 01:55:02 -0800
| Lines: 19
| Message-ID: <(E-Mail Removed)>
| 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.buildingc ontrols
| 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.buildingc ontrols:14322
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingc ontrols
|
| 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!
|

 
Reply With Quote
 
 
 
 
Samuel
Guest
Posts: n/a
 
      01-24-2006
======================================
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
 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      01-24-2006
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?= <(E-Mail Removed)>
| References: <(E-Mail Removed)>
<(E-Mail Removed)>
| 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: <(E-Mail Removed)>
| 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.buildingc ontrols
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGXA03.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.buildingc ontrols:14325
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.buildingc ontrols
|
| ======================================
| 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
|

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Paranoia - Windows Update causing scan of all mapped network drives upon Internet connection? ParanoiaPlus Computer Security 0 08-07-2005 04:47 PM
Struts and mapped property ShadowMan Java 1 12-04-2004 05:43 PM
Pass from parent to child, then update parent with child value... Noel Dolan Javascript 0 07-18-2004 05:52 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments