Unable to handle events of controls created at runtime

Discussion in 'ASP .Net Web Controls' started by Lalit, Dec 22, 2004.

  1. Lalit

    Lalit Guest

    Hi,

    My application has two methods which creates buttons at runtime. In one
    method I am able to handle the events of the buttons created at run time, but
    in other I am not.

    The code structure is as follows:

    page_load
    {
    ...Do something
    CreateFirstSetOfButtons()
    }

    private CreateFirstSetOfButtons()
    {
    // Create first set of buttons
    for { i=1; i<5; i++)
    {
    LinkButton newButton1 = new LinkButton();
    newButton1.Click += new EventHandler(this.Button1_Click);
    }
    ... Do something
    }

    // Event handler of first set of buttons. The second buttons are created on
    click of first buttons click.
    private void Button1_Click(object sender, System.EventArgs e)
    {
    CreateSecondSetOfButtons();
    }
    private CreateSecondSetOfButtons()
    {
    // Create first set of buttons
    for { i=1; i<2; i++)
    {
    LinkButton newButton2 = new LinkButton();
    newButton2.Click += new EventHandler(this.Button2_Click); // This event
    is never getting fired.
    }
    ... Do something
    }

    private void Button2_Click(object sender, System.EventArgs e)
    {
    Response.Write("Hello");
    }

    On click of buttons created at page load I am able to catch the Click evet
    of those buttons and the second set of buttons gets created. But when I click
    on this second set of created buttons, the statement
    Response.Write("Hello"); never gets executed.

    An early resolution for the same will be really helpful.

    Regards
    Lalit
    Lalit, Dec 22, 2004
    #1
    1. Advertising

  2. Lalit

    Guest

    I suspect this has to do with binding the event handlers too late.
    Take a look at:
    http://msdn.microsoft.com/library/d...guide/html/cpconcontrolexecutionlifecycle.asp

    While not crystal clear, I would guess that because your controls are
    present during the "LoadPostData" method, they aren't able to take
    part in post back events. I've seen a similar problem manifested in
    many ways while working with ASP.NET. In order for a dynamically
    created control to take part in PostBack and ViewState fully, it must
    be created and be part of the Page's Control hierarchy (either directly
    or as part of the Controls collection some other control which is
    eventually a part of the Page's Control collection).

    My role has been for the past few years to always create dynamc
    controls during the Init event.

    Hope this helps,
    Aidan
    , Dec 22, 2004
    #2
    1. Advertising

  3. Lalit

    Guest

    I suspect this has to do with binding the event handlers too late.
    Take a look at:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgu...

    While not crystal clear, I would guess that because your controls are
    not
    present during the "LoadPostData" method, they aren't able to take
    part in post back events. I've seen a similar problem manifested in
    many ways while working with ASP.NET. In order for a dynamically
    created control to take part in PostBack and ViewState fully, it must
    be created and be part of the Page's Control hierarchy (either directly
    or as part of the Controls collection some other control which is
    eventually a part of the Page's Control collection) by the time the
    Init event is done.

    My rule has been for the past few years to always create dynamc
    controls during the Init event.

    Hope this helps,
    Aidan
    , Dec 22, 2004
    #3
    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. =?Utf-8?B?TGFsaXQ=?=
    Replies:
    1
    Views:
    394
    Karl Seguin
    Dec 22, 2004
  2. newbye
    Replies:
    0
    Views:
    573
    newbye
    Jul 6, 2006
  3. Klaus Jensen
    Replies:
    1
    Views:
    505
    Yuriy Solodkyy
    Jun 22, 2007
  4. =?Utf-8?B?cml2YWxAbmV3c2dyb3Vwcy5ub3NwYW0=?=

    Linking events from controls, when the controls have been created

    =?Utf-8?B?cml2YWxAbmV3c2dyb3Vwcy5ub3NwYW0=?=, Jul 16, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    577
    =?Utf-8?B?cml2YWxAbmV3c2dyb3Vwcy5ub3NwYW0=?=
    Jul 16, 2007
  5. Lalit

    How to handle events of controls created at run time?

    Lalit, Dec 22, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    103
    Victor Garcia Aprea [MVP]
    Dec 23, 2004
Loading...

Share This Page