Adding events to dynamically created controls

Discussion in 'ASP .Net' 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. Jez,

    When you add dynamic controls to a page you have to re-create them on every
    page load in order for their events to fire.

    I have some samples of dynamic control use on my web site,
    www.aboutfortunate.com. I think they may be good for you to look at. Just
    click the code library link on the top right of the page and then enter
    "dynamic control" or something similar in the search box and you'll get all
    the relevant code.


    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer

    www.aboutfortunate.com

    "Out of chaos comes order."
    Nietzsche
    "JezB" <> wrote in message
    news:%...
    > 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 ?
    >
    >
    S. Justin Gengo, Jun 18, 2004
    #2
    1. Advertising

  3. JezB

    JezB Guest

    ah - true

    I was thinking that the event(s) fired before the page posted back, but I
    guess it's the other way round.

    "S. Justin Gengo" <sjgengo@aboutfortunate[no-spam].com> wrote in message
    news:...
    > Jez,
    >
    > When you add dynamic controls to a page you have to re-create them on

    every
    > page load in order for their events to fire.
    >
    > I have some samples of dynamic control use on my web site,
    > www.aboutfortunate.com. I think they may be good for you to look at. Just
    > click the code library link on the top right of the page and then enter
    > "dynamic control" or something similar in the search box and you'll get

    all
    > the relevant code.
    >
    >
    > --
    > Sincerely,
    >
    > S. Justin Gengo, MCP
    > Web Developer / Programmer
    >
    > www.aboutfortunate.com
    >
    > "Out of chaos comes order."
    > Nietzsche
    > "JezB" <> wrote in message
    > news:%...
    > > 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
    #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?Qm9i?=

    Events for Dynamically created controls

    =?Utf-8?B?Qm9i?=, Sep 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    348
    =?Utf-8?B?RGFyd2lu?=
    Sep 3, 2004
  2. Nathan Sokalski

    Handling Events for Dynamically Created Controls

    Nathan Sokalski, May 23, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    990
    Herfried K. Wagner [MVP]
    May 23, 2005
  3. SevDer
    Replies:
    2
    Views:
    1,300
    Walter Wang [MSFT]
    Nov 13, 2007
  4. msimmons
    Replies:
    0
    Views:
    472
    msimmons
    Jul 16, 2009
  5. Replies:
    10
    Views:
    153
Loading...

Share This Page