By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

How to remove dynamically created controls?

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.