How to remove dynamically created controls?

M

mawi

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>
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top