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

Events with dynamically added controls


Hi,

I am adding controls dynamically in a WebForm and would like to handle some of their events. Here is the code to set up the dynamic controls:

System.Web.UI.WebControls.TableRow row = new TableRow();
System.Web.UI.WebControls.TableCell cell = new TableCell();
System.Web.UI.WebControls.HyperLink btn = new HyperLink();
System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();

if (trk.Editable==true) {
btn.Text = "&nbsp" + "Edit";
btn.Target = "WebForm1.aspx";
img.ImageUrl = "Images/img_sec/edit_pencil.gif";
cell.Height = 5;
cell.Controls.Add(img);
cell.Controls.Add(btn);
row.Cells.Add(cell);
m_Table.Rows.Add(row);
} else {
btn.Visible = false;
img.Visible = false;
}

Any idea on how to do it?

Thanks a lot.
Mike

Nov 18 '05 #1
1 1266
Hi,

you can just wire event handlers in code.

For example for the Image:

img.Load += new EventHandler(<handler_method_name_here>);

which sets up a handler for the Load event of the Image control. Just
specify the method where I've put the < and > stuff.
--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist
http://blogs.aspadvice.com/joteke
"Mike" <no****@nospam.com> wrote in message
news:ut**************@TK2MSFTNGP12.phx.gbl...

Hi,

I am adding controls dynamically in a WebForm and would like to handle some
of their events. Here is the code to set up the dynamic controls:

System.Web.UI.WebControls.TableRow row = new TableRow();
System.Web.UI.WebControls.TableCell cell = new TableCell();
System.Web.UI.WebControls.HyperLink btn = new HyperLink();
System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();
if (trk.Editable==true) {
btn.Text = "&nbsp" + "Edit";
btn.Target = "WebForm1.aspx";
img.ImageUrl = "Images/img_sec/edit_pencil.gif";
cell.Height = 5;
cell.Controls.Add(img);
cell.Controls.Add(btn);
row.Cells.Add(cell);
m_Table.Rows.Add(row);
} else {
btn.Visible = false;
img.Visible = false;
}
Any idea on how to do it?
Thanks a lot.
Mike
Nov 18 '05 #2

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

Similar topics

0
by: Kenton Smeltzer | last post by:
Hello All, I am having a problem with events and the addition of controls on a page I am developing. First let me tell you what I have tried and then maybe someone can see something I missed. ...
3
by: Kenton Smeltzer | last post by:
Hello All, I am having a problem with events and the addition of controls on a page I am developing. First let me tell you what I have tried and then maybe someone can see something I missed. ...
1
by: hybrid | last post by:
I have problems in understanding the behavior of the events triggered by dynamically created controls over a webform. Could you help me? In a webform, I have a static PlaceHolder PH containing...
4
by: blue | last post by:
I have a drop-down list, a radio button list and a submit button. I'm adding these controls to a table and I'm adding the table to a Placeholder. I'm adding it to the Placeholder because I don't...
5
by: karthick raja | last post by:
Am experiencing a problem intercepting the events from controls added dynamically to a Placeholder control at runtime. Is there any way that I can write an event handler on the page which will be...
1
by: Bob | last post by:
I have a requirement to show a label and x number of buttons in all of the pages. So i decided to write a Base Page class and added code for dynamically adding the label and buttons. For the...
1
by: Paul | last post by:
An early Happy Thanksgiving to you and yours... We develop data driven apps. We dynamically place controls on a panel which may be then be placed on a web form or another control. My problem is...
8
by: Xero | last post by:
When my program is launched, there is a textbox on the form. When the user enters a character into the textbox (TextChanged), a second, declared textbox is added using this block of code. Dim...
0
by: seigo | last post by:
Hello, I faced with the following problem. I have a PlaceHolder on a page and a few UserControls which have custom events, for instance: public delegate void SelectHandler(object sender,...
2
by: Nathan Sokalski | last post by:
I have LinkButtons that are dynamically created in one of the PostBack events. They must be created in the PostBack event because one of the variables required to determine which ones to create...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.