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

eventhandlers for dynamically added controls

Hi NG

Im adding a bunch of linkbuttons to a page, in a for loop, but I cant get
the eventhandlers to work.

A simplyfied version of the code looks like this:

for (int i = 0; i<10; i++)
{
Button b = new Button();

b.Text = "button_"+i;

b.Click +=new EventHandler(b_Click);

buttons[i] = b;

i++;

TableCell tc = new TableCell();

tc.Controls.Add(b);

TableRow tr = new TableRow();

tr.Cells.Add(tc);

this.Table1.Rows.Add(tr);

}

And the b_Click method is defined like:
protected void b_Click(object sender, EventArgs e)

{

throw new Exception("Someone clicked one of the buttons");

}
The code is by memory, never mind syntax issues.

The code looks fine to me, but when the buttons are clicked the page just
reloads, the exception is never thrown, thus i figure the eventhandler
doesnt work.

Any surgestions?
- Kasper
Mar 8 '06 #1
1 1193
The controls and their event handler need to be created and hooked on
postback - no later than the page_load event.

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"Kasper Birch Olsen" <ka****@nospam.com> wrote in message
news:OL**************@tk2msftngp13.phx.gbl...
Hi NG

Im adding a bunch of linkbuttons to a page, in a for loop, but I cant get
the eventhandlers to work.

A simplyfied version of the code looks like this:

for (int i = 0; i<10; i++)
{
Button b = new Button();

b.Text = "button_"+i;

b.Click +=new EventHandler(b_Click);

buttons[i] = b;

i++;

TableCell tc = new TableCell();

tc.Controls.Add(b);

TableRow tr = new TableRow();

tr.Cells.Add(tc);

this.Table1.Rows.Add(tr);

}

And the b_Click method is defined like:
protected void b_Click(object sender, EventArgs e)

{

throw new Exception("Someone clicked one of the buttons");

}
The code is by memory, never mind syntax issues.

The code looks fine to me, but when the buttons are clicked the page just
reloads, the exception is never thrown, thus i figure the eventhandler
doesnt work.

Any surgestions?
- Kasper

Mar 8 '06 #2

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

Similar topics

3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
4
by: anders | last post by:
Hi! I have an application that looks something like this: ************************************************************ Order.aspx |--Thanks.ascx |--Step.ascx |--MultiView...
7
by: RichB | last post by:
I am trying to get to grips with the asp.net ajaxcontrol toolkit, and am trying to add a tabbed control to the page. I have no problems within the aspx file, and can dynamically manipulate a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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.