Control needs roundtrip to "reaffirm" eventhandler?

M

mawi

Hi there,

When removing page children controls created dynamically
not in last-to-first order, the close button of the last
control looses its event wiring, even though the handler is
rewired on each postback. It needs one postback roundtrip
to "get it back".

Form has an "add panel" button. Using it I dynamically add
3 panels with a remove button on each, A B C, to the page.
I remove B. Remove button on C then looses its click event.
Clicking it will do nothing, but the roundtrip will cause
it to get the event wiring back. If instead, I click a
"roundtrip" asp button that does nothing except initiates a
roundtrip, it will also get it back, and clicking on it
will remove the panel as expected.

Below is the code if anyone wants to try it out. If anyone
has any idea on this, please offer it to me! Thanks!

/mawi

<%@ Page language="c#" AutoEventWireup="true" Trace="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 Page_Load()
{
Trace.Write( "loadloadload" );
Button b;
Panel p;
foreach ( Control c in ph.Controls )
{
p = c as Panel;
if ( p != null )
{
Trace.Write( "panel" );
if ( ( b = c.Controls[ 2 ] as Button ) != null )
{
Trace.Write( "button" );
try
{
b.Click -= new System.EventHandler(this.RemovePanel);
}
finally
{
b.Click += new System.EventHandler(this.RemovePanel);
}
}
}
}
}
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;

String cnt = (Session[ "PanelCount" ] = ((int)Session[
"PanelCount" ]) + 1).ToString();

// b1.Click += new System.EventHandler(this.ChangeLabel);
l1.Text = "Persist me " + cnt;
b1.Text = "Change label " + cnt;
b1 = ph.Controls[ ix ].Controls[2] as Button;
b1.Text = "Remove panel " + cnt;
}
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;
}
protected void ReleaseSession(object sender,
System.EventArgs e)
{
Session.Abandon();
}
</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" />
<asp:Button Runat="server" OnClick="ReleaseSession"
Text="Abandon session" />
<asp:Button Runat="server" Text="Roundtrip" />
</form>
</body>
</html>
 
N

Natty Gur

Hi,

Here's the problem. Your code on RemovePanel remove panel from control
collection. now on the next time that you click remove, page recreate
controls but gives them other IDs (BTW it recommand to give your's
unique IDs). now your third panel get new ID that dosent exist in the
Request Form collection (you can check it out) therefor your Remove
button event won't happned. but your page recreate new ID's that now
match the panels thus in the next event your RemovePanel event execute.
you need to find a way to give your panels ID that you can "persiste"
over requests and use those names as buttons ID.

HTH

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)52-8888377


*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
M

mawi

Hello Natty Gur!

Big thanks, that did the trick. It was pretty sneaky, I was
actually aware that one is supposed to give unique IDs to
controls but since everything seemed to work alright, I
thought that it was something else.

I did try setting IDs at some point, but I just went
halfway. I modified the code to store a delimited string of
IDs in session state and now everything works.

Here is the code

http://authors.aspalliance.com/aylar/ViewPasteCode.aspx?PasteCodeID=2783

Once again, thanks alot. Really appreciate it!

/mawi
 

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,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top