Event of a dynamically added user control doesn't fire

Discussion in 'ASP .Net' started by niberhate, Apr 24, 2010.

  1. niberhate

    niberhate Guest

    I have MyUserControlClass.cs as follows. You will see that

    (1) LoadMyControl loads MyUserControl1, its CancelButtonEventHandler
    is delegated to myControl1_CancelButtonClicked.

    (2) In myControl1_CancelButtonClicked, MyUserControl2 is loaded, and
    its NewButtonClickEventHandler is delegated to
    myControl2_NewButtonClicked.

    CancelButtonClicked event fires with no problem and MyUserControl2 is
    successfully loaded upon firing.

    But, the New button's click event isn't fired, in other words,
    myControl2_NewButtonClicked doesn't get executed.

    Note that MyEventArgs has been correctly implemented. Any idea to
    share? Thanks.

    //////////////////////////////
    public partial class MyUserControlClass : System.Web.UI.UserControl
    {

    protected void Page_Load(object sender, EventArgs e)
    {
    LoadMyControl();
    }

    protected void LoadMyControl()
    {
    placeHolder1.Controls.Clear();
    var myControl1 = LoadControl("~/UserControls/
    MyUserControl1.ascx") as UserControls_MyUserControl1;
    myControl1.ID = "myControl1Id";
    myControl1.CancelButtonClicked += new
    CancelButtonClickEventHandler(myControl1_CancelButtonClicked);
    placeHolder1.Controls.Add(myControl1);
    }

    // myControl1_CancelButtonClicked gets executed as expected.
    protected void myControl1_CancelButtonClicked(object sender,
    MyEventArgs e)
    {
    var myControl2 = LoadControl("~/UserControls/
    MyUserControl2.ascx") as UserControls_MyUserControl2;
    myControl2.ID = "myControl2Id";
    myControl2.NewButtonClicked += new
    NewButtonClickEventHandler(myControl2_NewButtonClicked);
    placeHolder2.Controls.Clear();
    placeHolder2.Controls.Add(myControl2);
    }

    // New button click event either doesn't fire or doesn't get
    captured, and the following
    // method doesn't get executed.
    protected void myControl2_NewButtonClicked(object sender,
    MyEventArgs e)
    {
    // Do something here.
    }

    }
     
    niberhate, Apr 24, 2010
    #1
    1. Advertising

  2. niberhate

    Cubaman Guest

    Take a look here. It solved a similar problem I had some time ago.
    http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

    Best regards


    On Apr 24, 6:54 pm, niberhate <> wrote:
    > I have MyUserControlClass.cs as follows.  You will see that
    >
    > (1) LoadMyControl loads MyUserControl1, its CancelButtonEventHandler
    > is delegated to myControl1_CancelButtonClicked.
    >
    > (2) In myControl1_CancelButtonClicked, MyUserControl2 is loaded, and
    > its NewButtonClickEventHandler is delegated to
    > myControl2_NewButtonClicked.
    >
    > CancelButtonClicked event fires with no problem and MyUserControl2 is
    > successfully loaded upon firing.
    >
    > But, the New button's click event isn't fired, in other words,
    > myControl2_NewButtonClicked doesn't get executed.
    >
    > Note that MyEventArgs has been correctly implemented.  Any idea to
    > share? Thanks.
    >
    > //////////////////////////////
    > public partial class MyUserControlClass : System.Web.UI.UserControl
    > {
    >
    >     protected void Page_Load(object sender, EventArgs e)
    >     {
    >         LoadMyControl();
    >     }
    >
    >     protected void LoadMyControl()
    >     {
    >         placeHolder1.Controls.Clear();
    >         var myControl1 = LoadControl("~/UserControls/
    > MyUserControl1.ascx") as UserControls_MyUserControl1;
    >         myControl1.ID = "myControl1Id";
    >         myControl1.CancelButtonClicked += new
    > CancelButtonClickEventHandler(myControl1_CancelButtonClicked);
    >         placeHolder1.Controls.Add(myControl1);
    >     }
    >
    >    // myControl1_CancelButtonClicked gets executed as expected.
    >     protected void myControl1_CancelButtonClicked(object sender,
    > MyEventArgs e)
    >     {
    >         var myControl2 = LoadControl("~/UserControls/
    > MyUserControl2.ascx") as UserControls_MyUserControl2;
    >         myControl2.ID = "myControl2Id";
    >         myControl2.NewButtonClicked += new
    > NewButtonClickEventHandler(myControl2_NewButtonClicked);
    >         placeHolder2.Controls.Clear();
    >         placeHolder2.Controls.Add(myControl2);
    >     }
    >
    >    // New button click event either doesn't fire or doesn't get
    > captured, and the following
    >    // method doesn't get executed.
    >     protected void myControl2_NewButtonClicked(object sender,
    > MyEventArgs e)
    >     {
    >         // Do something here.
    >     }
    >
    > }
     
    Cubaman, Apr 26, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Henke
    Replies:
    1
    Views:
    490
  2. Bennett Haselton
    Replies:
    1
    Views:
    543
    John M Deal
    Nov 8, 2004
  3. Jon B
    Replies:
    5
    Views:
    5,260
  4. Jon B

    Handle Event of Dynamically Added User Control

    Jon B, Apr 29, 2005, in forum: ASP .Net Building Controls
    Replies:
    5
    Views:
    183
  5. Replies:
    0
    Views:
    149
Loading...

Share This Page