473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to remove dynamically created controls?

Hello,

Description: I create panels with some controls on a page
using a new panel button. One of the controls on each panel
is the "close panel" button that is supposed to close the
panel it is on. This works fine only if one closes the
panels from last panel. Otherwise two clicks is necessary
for the last panel - ie, if you press add 3 times, close 2
and then 3 - two clicks on 3 will be necessary.

Recreate: Please cut and paste complete aspx code below
into a webserver directory and browse it to see what I
mean, harder to explain than to test!

(start by doing it the working way; click add 3 times,
click close buttons from bottom. When all are closed, add a
3 new ones and then try closing one in the middle first,
you'll see the problem...)

Problem: When removing (children of page) controls created
dynamically not in last-to-first order, the close button of
the last control seems to loose its event wiring, even
though the handler is rewired on each postback.
Please help me out on this guys - I am puzzled!

/mawi

CODE TO RECREATE, complete aspx file (notepad or
snippetcompiler friendly ;) :

<%@ Page language="c#" AutoEventWireup="true" %>
<script runat="server">
void Page_Init()
{
if ( Session[ "PanelCount" ] == null )
{
Session[ "PanelCount" ] = 0;
AddPanel( null, null );
}
else
{
int panelCnt = (int)Session["PanelCount" ];
for ( int i = 0; i < panelCnt; i++ )
AddPanel();
}
}
void ChangeLabel(object sender, System.EventArgs e)
{
((sender as Button).Parent.Controls[ 0 ] as Label).Text
= "I got changed";
}
protected void AddPanel( object sender, System.EventArgs e )
{
AddPanel();
int ix = ph.Controls.Count - 1;
if ( ix < 0 )
ix = 0;
Label l1 = ph.Controls[ ix ].Controls[0] as Label;
Button b1 = ph.Controls[ ix ].Controls[1] as Button;
// b1.Click += new System.EventHandler(this.ChangeLabel);
l1.Text = "Persist me";
b1.Text = "Change label";
b1 = ph.Controls[ ix ].Controls[2] as Button;
b1.Text = "Remove panel";

Session[ "PanelCount" ] = ((int)Session[ "PanelCount" ])
+ 1;
}
private void AddPanel()
{
Panel p1 = new Panel();
ph.Controls.Add( p1 );
Button b1 = new Button();
p1.Controls.Add( new Label() );
p1.Controls.Add( b1 );
b1.Click += new System.EventHandler(this.ChangeLabel);
// comment this to try event persist
Button b = new Button();
p1.Controls.Add( b );
b.Click += new System.EventHandler(this.RemovePanel);
}
protected void RemovePanel(object sender, System.EventArgs e)
{
( sender as Button ).Parent.Parent.Controls.Remove( (
sender as Button ).Parent );
Session[ "PanelCount" ] = ((int)Session[ "PanelCount" ])
- 1;
}

</script>
<html>
<body>
<form runat="server">
<asp:PlaceHolder id="ph" runat="server" />
<asp:Button id="adder" Runat="server" OnClick="AddPanel"
Text="add panel" AccessKey="a" />
</form>
</body>
</html>

Nov 18 '05 #1
0 2037

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

Similar topics

3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
0
by: John Crowley | last post by:
I'm having an odd problem with viewstate and a dynamically created control inside a repeater template. Basically, I have a repeater setup like this in the aspx:
2
by: R Duke | last post by:
I have tried everything I can think of to change the visible property of a design time created control from a dynamically created control's command event handler. Here is the scenario. I have...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
1
by: Marcus | last post by:
I have a problem maybe one of you could help me with. I've created a data entry screen with lots of dynamically-created client-side controls. I create HTML texboxes client-side by assigning a...
12
by: nat | last post by:
Hi all, I was going to post this in the AJAX forum, but apparently someone posted an AJAX/C# question in there today and was told to try this forum instead. So here goes: I'm trying to figure...
9
by: Chris | last post by:
I am dynamically adding a user control to each row in a gridview. The reason I am doing it dynamically is the user control is different depending on certain data in the gridview. The gridview...
4
by: mohaaron | last post by:
I can think of a lot of reasons why this might need to be done but as far as I can tell it's not possible. I've been looking for a way to add HtmlTableRows to a table using a button click for a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.