Events with dynamically added controls


M

Mike

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
 
Ad

Advertisements

T

Teemu Keiski

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



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top