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

Adding events to dynamically created controls

P: n/a
I'm adding WebControl objects to a Page dynamically on Page_Load, but I'm
having trouble attaching events to these. For example, adding an image
button :-

ImageButton nb = new ImageButton();
nb.ImageUrl = "text.gif";
nb.ToolTip = "Edit Text";
nb.Click += new ImageClickEventHandler(b1_Click);
myPlaceholder.Controls.Add(nb);

(myPlaceholder is a Placeholder object added to the Page at design time)

private void b1_Click(object sender, ImageClickEventArgs e)
{
// etc
}

Problem is, on clicking the button the event is not fired at all - it just
forces a postback - what am I doing wrong ?
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Jez,

When you add dynamic controls to a page you have to re-create them on every
page load in order for their events to fire.

I have some samples of dynamic control use on my web site,
www.aboutfortunate.com. I think they may be good for you to look at. Just
click the code library link on the top right of the page and then enter
"dynamic control" or something similar in the search box and you'll get all
the relevant code.
--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"JezB" <je***********@blueyonder.co.uk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I'm adding WebControl objects to a Page dynamically on Page_Load, but I'm
having trouble attaching events to these. For example, adding an image
button :-

ImageButton nb = new ImageButton();
nb.ImageUrl = "text.gif";
nb.ToolTip = "Edit Text";
nb.Click += new ImageClickEventHandler(b1_Click);
myPlaceholder.Controls.Add(nb);

(myPlaceholder is a Placeholder object added to the Page at design time)

private void b1_Click(object sender, ImageClickEventArgs e)
{
// etc
}

Problem is, on clicking the button the event is not fired at all - it just
forces a postback - what am I doing wrong ?

Nov 18 '05 #2

P: n/a
ah - true

I was thinking that the event(s) fired before the page posted back, but I
guess it's the other way round.

"S. Justin Gengo" <sjgengo@aboutfortunate[no-spam].com> wrote in message
news:10*************@corp.supernews.com...
Jez,

When you add dynamic controls to a page you have to re-create them on every page load in order for their events to fire.

I have some samples of dynamic control use on my web site,
www.aboutfortunate.com. I think they may be good for you to look at. Just
click the code library link on the top right of the page and then enter
"dynamic control" or something similar in the search box and you'll get all the relevant code.
--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"JezB" <je***********@blueyonder.co.uk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
I'm adding WebControl objects to a Page dynamically on Page_Load, but I'm having trouble attaching events to these. For example, adding an image
button :-

ImageButton nb = new ImageButton();
nb.ImageUrl = "text.gif";
nb.ToolTip = "Edit Text";
nb.Click += new ImageClickEventHandler(b1_Click);
myPlaceholder.Controls.Add(nb);

(myPlaceholder is a Placeholder object added to the Page at design time)

private void b1_Click(object sender, ImageClickEventArgs e)
{
// etc
}

Problem is, on clicking the button the event is not fired at all - it just forces a postback - what am I doing wrong ?


Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.