Responding to events in dynamically created web controls

Discussion in 'ASP .Net Web Controls' started by JezB, Jun 18, 2004.

  1. JezB

    JezB Guest

    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 ?
    JezB, Jun 18, 2004
    #1
    1. Advertising

  2. JezB

    Joe.Dattilo Guest

    The Server Side Event Handling happens before the page_load event and after the On_init ... add your controls and event handler hookups in the On_Init Event and you should be able to get the events to fire.

    Joe

    "JezB" wrote:

    > 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 ?
    >
    >
    >
    >
    Joe.Dattilo, Jun 18, 2004
    #2
    1. Advertising

  3. This is OF COURSE NOT true. All events happen AFTER Page_Load event. The
    thing is, that you developers sometimes don't know how the process happens
    in the first place.
    All dynamic controls should be restored the same way they were created the
    first time. And you have to recreate them BEFORE the events get processed.
    So the latest possible time to recreate those is Page_Load.
    What JoeDattilo suggested is the way to process ViewState, not events.
    Because viewstate gets processed after OnInit and BEFORE PageLoad. If you
    don't need that, then forget about it...

    --
    RobertK
    { Clever? No just smart. }

    "Joe.Dattilo" <> wrote in message
    news:...
    > The Server Side Event Handling happens before the page_load event and

    after the On_init ... add your controls and event handler hookups in the
    On_Init Event and you should be able to get the events to fire.
    >
    > Joe
    >
    > "JezB" wrote:
    >
    > > 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 ?
    > >
    > >
    > >
    > >
    Robert Koritnik, Jun 21, 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. JezB
    Replies:
    2
    Views:
    4,828
  2. =?Utf-8?B?Qm9i?=

    Events for Dynamically created controls

    =?Utf-8?B?Qm9i?=, Sep 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    337
    =?Utf-8?B?RGFyd2lu?=
    Sep 3, 2004
  3. carhip
    Replies:
    0
    Views:
    333
    carhip
    Sep 8, 2008
  4. msimmons
    Replies:
    0
    Views:
    444
    msimmons
    Jul 16, 2009
  5. colter k?ntz via .NET 247

    responding to events raised by controls in a webcontrol

    colter k?ntz via .NET 247, Jun 15, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    129
    Robert
    Jun 15, 2004
Loading...

Share This Page