473,416 Members | 1,712 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,416 software developers and data experts.

Unable to handle events of controls created at run time.

Hi,

My application has two methods which creates buttons at runtime. In one
method I am able to handle the events of the buttons created at run time, but
in other I am not.

The code structure is as follows:

page_load
{
...Do something
CreateFirstSetOfButtons()
}

private CreateFirstSetOfButtons()
{
// Create first set of buttons
for { i=1; i<5; i++)
{
LinkButton newButton1 = new LinkButton();
newButton1.Click += new EventHandler(this.Button1_Click);
}
... Do something
}

// Event handler of first set of buttons. The second buttons are created on
click of first buttons click.
private void Button1_Click(object sender, System.EventArgs e)
{
CreateSecondSetOfButtons();
}
private CreateSecondSetOfButtons()
{
// Create first set of buttons
for { i=1; i<2; i++)
{
LinkButton newButton2 = new LinkButton();
newButton2.Click += new EventHandler(this.Button2_Click); // This event
is never getting fired.
}
... Do something
}

private void Button2_Click(object sender, System.EventArgs e)
{
Response.Write("Hello");
}

On click of buttons created at page load I am able to catch the Click evet
of those buttons and the second set of buttons gets created. But when I click
on this second set of created buttons, the statement
Response.Write("Hello"); never gets executed.

An early resolution for the same will be really helpful.

Regards
Lalit

Nov 19 '05 #1
1 1518
Lalit:
The 2nd event isn't firing because the 2nd buttons aren't being re-added.

Page_loads
--> First buttons created
First Button Clicked
--> First buttons recreated
--> Second buttons created
Second Button clicked
--> First buttons recreated
--> Second buttons NOT recreated

The 2nd buttons aren't being readded because they are only added when the
first buttons are clicked, which they weren't on the last trip. To solve
the issue you need to create the 2nd set of buttons under 2
circumstances...when the 1st set is clicked (Which you are doing) AND when
the 2nd set is clicked...

You might wanna check out
http://www.denisbauer.com/ASPNETCont...aceholder.aspx
(free) for help.

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Lalit" <La***@discussions.microsoft.com> wrote in message
news:4D**********************************@microsof t.com...
Hi,

My application has two methods which creates buttons at runtime. In one
method I am able to handle the events of the buttons created at run time, but in other I am not.

The code structure is as follows:

page_load
{
...Do something
CreateFirstSetOfButtons()
}

private CreateFirstSetOfButtons()
{
// Create first set of buttons
for { i=1; i<5; i++)
{
LinkButton newButton1 = new LinkButton();
newButton1.Click += new EventHandler(this.Button1_Click);
}
... Do something
}

// Event handler of first set of buttons. The second buttons are created on click of first buttons click.
private void Button1_Click(object sender, System.EventArgs e)
{
CreateSecondSetOfButtons();
}
private CreateSecondSetOfButtons()
{
// Create first set of buttons
for { i=1; i<2; i++)
{
LinkButton newButton2 = new LinkButton();
newButton2.Click += new EventHandler(this.Button2_Click); // This event is never getting fired.
}
... Do something
}

private void Button2_Click(object sender, System.EventArgs e)
{
Response.Write("Hello");
}

On click of buttons created at page load I am able to catch the Click evet
of those buttons and the second set of buttons gets created. But when I click on this second set of created buttons, the statement
Response.Write("Hello"); never gets executed.

An early resolution for the same will be really helpful.

Regards
Lalit

Nov 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: a ghost1207 | last post by:
Hi all, I am trying to figure this out, but I can't: I create a form, then another form (which I call from the first), then add an event handler for KeyDown messages in the second form and...
2
by: luca | last post by:
I'm trying to build a Server Control, it's a calendar to manage sellers appointments (don't answer me to use and custumize Calendar Control because unluckily it's not possible for this specific...
2
by: Michael Ramey | last post by:
Howdy, I think I have a good question! I'm creating a usercontrol, and within this usercontrol, I'm dynamically creating controls (imagebuttons to be exact), that the user can click on which...
0
by: Scott McChesney | last post by:
I have a problem I hope you folks can help me with. I have an application that is using a tab-based interface, with the ability for users to drag an item from a ListBox onto the tab control. ...
15
by: Adam J. Schaff | last post by:
I have noticed that if a user closes a form via pressing return (either while the OK button has focus or if AcceptButton is set to OK for the form) then the "ENTER" keypress event fires ON THE...
5
by: Steve Moreno | last post by:
Hi all, I've got a web form that I've written code to create an array of DropDownList controls on the page depending on how many records are pulled back. The code to create the controls is...
7
by: Bruce HS | last post by:
I'd like to call my ancestor Validation Function every time any control on a Win Form generates a Validating or Validated event. I'm using VB. I've extended Textbox, for instance, to have its...
6
by: Steve Hershoff | last post by:
Hi everyone, I've got a strange one here. There are two datagrids on my page, one nested within the other. I'll refer to them as the topmost and secondary datagrids. In the topmost...
3
by: Jose Fernandez | last post by:
Hello. I would like to know how could i get all the subscriptions that my form has with the events of their controls. For example. I have a form with a textbox, a button and a dropdown. I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.