Order is important in CreateChildControls
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcontrolscs' list.


Peter Blum
I had struggled with the following issue for a while and have received an
answer from Microsoft (very quickly too!) I though you might appreciate the
knowledge.
A ListBox control restores its Items list automatically on Post Back when
that control is declared directly in the ASP.NET code.
So you can write:
<script>
protected void Page_Load()
{
if (!IsPostBack)
{
MyList.Items.Add("Item1");
MyList.Items.Add("Item2");
}
}
</script>
<....HTML stuff...>
<asp:ListBox id=MyList runat="server" />

Inside a Composite Control, you can do the same in the CreateChildControls
method. However, there is an order to things. You must set any properties
that are preserved by the control AFTER adding it to the parent's control
list.
This preserves the items in a list box:
MyList = new ListBox()
Controls.Add(MyList )
MyList.Items.Add("Item1");
MyList.Items.Add("Item2");
If you move Controls.Add(MyList) to the bottom, it does not preserve the
items.
Unfortunately, the MSDN documentation has shown assigning of properties
before the Controls.Add() call, at least in the primary example of Composite
Controls: "Composite Server Control Sample".

--- Peter

Reply to this message...
 
    
Vandana Datye (DevCepts)
In general, the order shown in the doc sample within CreateChildControls
is the way to go, because you want to minimize the view state. You
should perform initialization before adding to the control tree.

However, in your case, you need to persist the items in view state
because they are not added to the ListBox every time but only when the
page is first requested. Therefore they must be round-tripped.=20

Vandana

-----Original Message-----
From: Peter Blum [mailto:Click here to reveal e-mail address]=20
Sent: Wednesday, April 24, 2002 12:08 PM
To: aspngcontrolscs
Subject: [aspngcontrolscs] Order is important in CreateChildControls

I had struggled with the following issue for a while and have received
an
answer from Microsoft (very quickly too!) I though you might appreciate
the
knowledge.
A ListBox control restores its Items list automatically on Post Back
when
that control is declared directly in the ASP.NET code.
So you can write:
<script>
protected void Page_Load()
{
if (!IsPostBack)
{
MyList.Items.Add("Item1");
MyList.Items.Add("Item2");
}
}
</script>
<....HTML stuff...>
<asp:ListBox id=3DMyList runat=3D"server" />

Inside a Composite Control, you can do the same in the
CreateChildControls
method. However, there is an order to things. You must set any
properties
that are preserved by the control AFTER adding it to the parent's
control
list.
This preserves the items in a list box:
MyList =3D new ListBox()
Controls.Add(MyList )
MyList.Items.Add("Item1");
MyList.Items.Add("Item2");
If you move Controls.Add(MyList) to the bottom, it does not preserve the
items.
Unfortunately, the MSDN documentation has shown assigning of properties
before the Controls.Add() call, at least in the primary example of
Composite
Controls: "Composite Server Control Sample".

--- Peter

| [aspngcontrolscs] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspngcontrolscs.asp =3D JOIN/QUIT
| http://www.asplists.com/search =3D SEARCH Archives

Reply to this message...
 
 
System.Web.UI.WebControls.ListBox
System.Windows.Forms.ListBox




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com