Adding eventhandler dynamically to usercontrol

Discussion in 'ASP .Net Web Controls' started by Simon Maltow, Aug 1, 2005.

  1. Simon Maltow

    Simon Maltow Guest

    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";
    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
    Simon Maltow, Aug 1, 2005
    1. Advertisements

  2. 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 handles
    postback, since that is where the event is determined and the control

    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?
    Andrew Backer, Aug 1, 2005
    1. Advertisements

  3. Simon Maltow

    sim Guest

    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.


    Andrew Backer skrev:
    sim, Aug 3, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.