473,508 Members | 2,298 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Keeping added subcontrols through postbacks

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

Nov 19 '05 #1
2 994
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/ASPNETCont...aceholder.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!)
<ja*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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

Nov 19 '05 #2
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

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
12487
by: Martin Schulze | last post by:
Hello, i tried to compose myself a custom usercontrol which is derieved from System.Windows.Forms.UserControl. It contains 2 comboboxes and one textbox (which are also custom controls, but...
6
1943
by: foldface | last post by:
Hi If I add a UserControl to a placeholder on a page during that pages PreRender stage and press a button on that UserControl will the postback for that button get processed? I would have thought...
2
2012
by: Mark | last post by:
I'm adding several controls (labels, etc.) to a placeholder programatically. I'd like the PlaceHolder to maintain these contents during postbacks without having to programmatically recreate their...
2
1046
by: Dimitris Pantazopoulos | last post by:
Ok, let's say I recreate the dynamically-added Dropdownlist controls of my web form even at postbacks. That means they are repopulated with values as well. (If I don't I won't be getting anything...
1
2286
by: Alan Silver | last post by:
Hello, I have a page in which I'm trying to give the user the chance to manipulate a list of items. These are the price variations for a product, so each item consists of a name (eg, small,...
7
2583
by: David Lozzi | last post by:
Howdy, I have a usercontrol in my aspx page and when the page loads, I send it some property values based on the data displayed. I am sending it to a public property like below. However the...
5
3713
by: Dennis Fazekas | last post by:
Greetings, I am creating a web form which will all the user to add an unlimited number of email addresses. Basically I have 3 buttons, "Add Another Email", "-" to remove, and a "Save" button....
0
1432
by: Rune Jacobsen | last post by:
Hi all, In my project I have a control (described in an earlier post) which basically consists of three "subcontrols": One big area where the vital data is displayed (scrollable, since it will...
7
2119
by: Nathan Sokalski | last post by:
I have a page which I dynamically add several usercontrols (*.ascx files) to using the following code: Public Sub Refresh() For Each section As DataRow In Me.GetSections().Rows...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7495
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.