By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,712 Members | 1,413 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,712 IT Pros & Developers. It's quick & easy.

Unable to handle events of controls created at run time.

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.