473,791 Members | 2,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Row s.Add(tr);

}

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

{

throw new Exception("Some one 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 1209
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******** ******@tk2msftn gp13.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.Row s.Add(tr);

}

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

{

throw new Exception("Some one 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
10808
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 then like that, when clicking the LinkButton, the user can be navigated to another page, carrying a variable. I would like to use server.transfer method instead of QueryString as I don't want the carried variable to be visible for the user.
3
390
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 then like that, when clicking the LinkButton, the user can be navigated to another page, carrying a variable. I would like to use server.transfer method instead of QueryString as I don't want the carried variable to be visible for the user.
4
4939
by: anders | last post by:
Hi! I have an application that looks something like this: ************************************************************ Order.aspx |--Thanks.ascx |--Step.ascx |--MultiView |--(View1)Whois.ascx (contains EventHandler's) |--(View2)Webbpaket.ascx
7
6672
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 tabcontainer which has 1 panel already, however I want to try create the TabPanels dynamically. I followed the advice here: http://www.asp.net/learn/ajax-videos/video-156.aspx (3rd comment - Joe Stagner)
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10154
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9993
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.