468,243 Members | 2,024 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,243 developers. It's quick & easy.

Control needs roundtrip to "reaffirm" eventhandler?

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>

Nov 18 '05 #1
2 1288
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!
Nov 18 '05 #2
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...steCodeID=2783

Once again, thanks alot. Really appreciate it!

/mawi
-----Original Message-----
Hi,

Here's the problem. Your code on RemovePanel remove panel from controlcollection. now on the next time that you click remove, page recreatecontrols but gives them other IDs (BTW it recommand to give your'sunique IDs). now your third panel get new ID that dosent exist in theRequest Form collection (you can check it out) therefor your Removebutton event won't happned. but your page recreate new ID's that nowmatch 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!
.

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Josef Meile | last post: by
5 posts views Thread by Joe Fallon | last post: by
3 posts views Thread by mortb | last post: by
4 posts views Thread by Jim Reynolds | last post: by
3 posts views Thread by Marc Solé | last post: by
5 posts views Thread by =?Utf-8?B?cm9kY2hhcg==?= | last post: by
4 posts views Thread by Steven Nagy | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.