Adding eventhandler dynamically to usercontrol

S

Simon Maltow

Hi there,

I have an aspx page where I load a user control at design-time. I then
want to add a LinkButton to this user control and attach an
eventhandler. I want the LinkButton's click event to be handled on the
parent page and not on the user control.

My code looks like this on the parent page:

LinkButton lbSendMail = new LinkButton();
PlaceHolder tmp = (PlaceHolder)ctrDynamicContent.FindControl("phLinkContainer");
lbSendMail.Text = "SomeText";
lbSendMail.ID = "lbSendMail";
tmp.Controls.Add(lbSendMail);
LinkButton tmpBtn = (LinkButton)ctrDynamicContent.FindControl("lbSendMail");
tmpBtn.Click += new EventHandler(HandleClickEvent);
ctrDynamicContent.Visible = true;

I don't get any errors but the event does not get handled...

Any ideas to how this can be done ?

Thanks for any replies
Simon Maltow
 
A

Andrew Backer

Hopefully someone smarter than I will respond! Can you tell us where
this code lives, so we have an idea when it gets executed? I belive
that everything has to be set up (created, etc) before asp.net handles
postback, since that is where the event is determined and the control
located.

If you create this control early enough (not sure if it's possible
since it's all dynamic), and it has the same ID, you may be ok.

Have you considered using using a more generic handler and a
commandName/commandArgument to do this, rather than trying to attach to
a transient control like this?
 
S

sim

Thanks for your reply Andrew !

The code lives in an eventhandler method on a page containing several
user controls. The eventhandler gets triggered by an event occurring in
another user control. I think you're right about creating everything
before it can get handled on postback. The problem is I don't know how
to recreate the added control on postback. As of now I have chosen a
simpler solution (time pressure) but I'd still like to make this a more
dynamic control.

Regards
Simon


Andrew Backer skrev:
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top