473,402 Members | 2,046 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,402 software developers and data experts.

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 1448
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Josef Meile | last post by:
Hi I wanted to programmatically disable events like SelectedIndexChanged without using a boolean flag, which indeed I find a quick and dirty solution. So, I found a piece of code that do this...
5
by: Joe Fallon | last post by:
I have a user control which can appear on many pages. The control needs to know which page it is on so it can render itself correctly. What is the best way for the control to find out what page is...
3
by: mortb | last post by:
HI all, I'm writing a control that resides on a page which instantiates an object from the database. My goal is to make the control use the same object for the control as on the page....
4
by: Jim Reynolds | last post by:
Hi I am trying to raise a user control event but in the test the event is always null so never fires. Why is this please? public event EventHandler Click; private void butOK_Click(object...
1
by: Martin | last post by:
Hi, I have produced a custom server control that simple outputs a row of 26 buttons, one button for each letter of the english alphabet. now what I would like to do is catch the button click...
6
by: Oscar | last post by:
I want to add items to a dropdownlist control within a Javascript eventhandler. This is what I code : var dd = document.getElementById("DropDownList1"); dd.Items.Add("1990");...
3
by: Marc Solé | last post by:
Hello to everybody. I have a little problem with a Custom Control that I have created. The thing is that in my control, when I click a button, it should create another object in the main form. ...
5
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i have a simple web page that has one DropDownList that contains employees. The page also has a WebUserControl that displays a GridView of records related to the...
4
by: Steven Nagy | last post by:
Hi all, I have a custom user control in an ASP.NET app (.NET 2.0). This control needs a public property called "ControlToValidate" which accepts strings. I need to be able to declare it much as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.