Keeping added subcontrols through postbacks

J

jand187

Hi,

I'm using ASP.Net 2.0 Beta 2.

I have an asp:panel that works as a list. I have a button that adds an
item to that list. When i click on the button I want a new item
(UserControl ASCX) to be added to the list.

But every time i click on the button it seems my list is cleared and
only the newest item is added to the list.

The idea is to start with an empty list and then the user can choose
among several types (ascx) through a normal dropdown and add them to
the list by clicking the "Add" button, but somehow i cannot get my
PanelControl to hold the other controls already added by previous
clicks on the add button.

ASPX:
<form id="form1" runat="server">
<asp:Button runat="server" ID="btn_Add" Text="Add"
OnClick="btn_Add_Click" />
<asp:panel ID="pnl_ItemList" runat="server" Height="300px"
Width="400px" style="overflow-y: auto;" />
</form>

CS:

protected void btn_Add_Click( object sender, EventArgs e )
{
UserControl item = (UserControl) LoadControl( "Item.ascx" );
pnl_ItemList.Controls.Add( item );
}

I think i need to do some Viewstate stuff, but the new ASP.Net 2.0
eludes me. It seems like a lot of stuff is hidden (like the
eventhandler setup).

Please let me know if you have an idea.

Thanx in advance.
JanD
 
K

Karl Seguin

This is the same behaviour you'd get in 1.1...dynamically added controls
need to be re-added on each postback. They aren't preserved in viewstate
(controls are never preservered in viewstate, only control values).

Denis Bauer has a DynamicPlaceHolder control available for free that takes
care of re-recreating controls for you. not sure if it works in 2.0...
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
J

JanD

Thx Karl,

Yeah, it's just me being stupid. I saw the behavior that i needed in
the framework, but after a while (slow today) i noticed that it worked
on values, not controls. I've written my own little Viewstate thingy
that recreates the behavior for my controls. Thx for you reply though
:)

-JanD
 

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
473,770
Messages
2,569,588
Members
45,093
Latest member
Vinaykumarnevatia00

Latest Threads

Top