dynamic linkbuttons event not raising - newbie

Discussion in 'ASP .Net Building Controls' started by Matias Woloski, Oct 18, 2004.

  1. Hi, I am creating a custom server control, and it dynamically creates lots
    of LinkButtons, and then I try to subscribe to the click event doing this:
    LinkButton link = new LinkButton();
    link.Click +=new EventHandler(this.MyLinks_Click);

    When I then click on the link, it never invokes MyLinks_Clicks... I guess
    this happens because I don't create the linkbuttons neither on
    CreateChildControls() nor OnInit(). I create them depending on some input
    data and some properties.
    All that MyLinks(...) does is set a property to a value, depending on the
    linkbutton pressed, beacuse I need it when I render again my control.
    Can someone give me a hint on how to solve this problem? Maybe instead of
    subscribing to the event, I can catch the name of the linkbutton that caused
    the PostBack or something.
    Thanks
     
    Matias Woloski, Oct 18, 2004
    #1
    1. Advertising

  2. Matias Woloski

    A Traveler Guest

    Hello, i dont think your problem has anything to do with adding the OnClick
    handler not in CCC or in OnInit. I have a number of pages which add dynamic
    controls and bind them up to an eventhandler then and there. It doesnt
    usually matter too much where you do that.

    You could try checking the form postback. You would look in
    Request("__EVENTTARGET") and see if it contains the name of your control.
    The event handlers is definitely the better way to go though, and it should
    work.
    I dont know if this is just a typo or not, but in your post, you said you
    add new EventHandler(MyLinks_Click), but that it never executes
    MyLinks_Clicks, with an "s" on the end. Maybe that is it? or its just a
    typo.


    "Matias Woloski" <> wrote in message
    news:...
    > Hi, I am creating a custom server control, and it dynamically creates lots
    > of LinkButtons, and then I try to subscribe to the click event doing this:
    > LinkButton link = new LinkButton();
    > link.Click +=new EventHandler(this.MyLinks_Click);
    >
    > When I then click on the link, it never invokes MyLinks_Clicks... I guess
    > this happens because I don't create the linkbuttons neither on
    > CreateChildControls() nor OnInit(). I create them depending on some input
    > data and some properties.
    > All that MyLinks(...) does is set a property to a value, depending on the
    > linkbutton pressed, beacuse I need it when I render again my control.
    > Can someone give me a hint on how to solve this problem? Maybe instead of
    > subscribing to the event, I can catch the name of the linkbutton that
    > caused
    > the PostBack or something.
    > Thanks
    >
    >
     
    A Traveler, Oct 18, 2004
    #2
    1. Advertising

  3. Matias Woloski

    Skok Tone Guest

    You have to add control to control collection of custom control for events
    to work.

    "Matias Woloski" <> wrote in message
    news:...
    > Hi, I am creating a custom server control, and it dynamically creates lots
    > of LinkButtons, and then I try to subscribe to the click event doing this:
    > LinkButton link = new LinkButton();
    > link.Click +=new EventHandler(this.MyLinks_Click);
    >
    > When I then click on the link, it never invokes MyLinks_Clicks... I guess
    > this happens because I don't create the linkbuttons neither on
    > CreateChildControls() nor OnInit(). I create them depending on some input
    > data and some properties.
    > All that MyLinks(...) does is set a property to a value, depending on the
    > linkbutton pressed, beacuse I need it when I render again my control.
    > Can someone give me a hint on how to solve this problem? Maybe instead of
    > subscribing to the event, I can catch the name of the linkbutton that
    > caused
    > the PostBack or something.
    > Thanks
    >
    >
     
    Skok Tone, Oct 18, 2004
    #3
  4. Hi Matias,

    >>> I create them depending on some input
    >>> data and some properties.


    Where are you creating them? In response to a postback event? If so, then
    its too late for them to fire any event at all. You need to move creation
    earlier in the page lifecycle. Also, you will need to recreate the
    linkbutton(s) on postback *even* if you don't need them this time in order
    to get the event fired, you can then -before rendering- remove them.

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://clariusconsulting.net/vga
    My profile: http://aspnet2.com/mvp.ashx?vga



    "Matias Woloski" <> wrote in message
    news:...
    > Hi, I am creating a custom server control, and it dynamically creates lots
    > of LinkButtons, and then I try to subscribe to the click event doing this:
    > LinkButton link = new LinkButton();
    > link.Click +=new EventHandler(this.MyLinks_Click);
    >
    > When I then click on the link, it never invokes MyLinks_Clicks... I guess
    > this happens because I don't create the linkbuttons neither on
    > CreateChildControls() nor OnInit(). I create them depending on some input
    > data and some properties.
    > All that MyLinks(...) does is set a property to a value, depending on the
    > linkbutton pressed, beacuse I need it when I render again my control.
    > Can someone give me a hint on how to solve this problem? Maybe instead of
    > subscribing to the event, I can catch the name of the linkbutton that
    > caused
    > the PostBack or something.
    > Thanks
    >
    >
     
    Victor Garcia Aprea [MVP], Oct 23, 2004
    #4
    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. Rick Glos
    Replies:
    0
    Views:
    652
    Rick Glos
    Jul 8, 2003
  2. Jitu
    Replies:
    0
    Views:
    404
  3. prashant khandalkar via DotNetMonster.com

    linkbutton not raising event.

    prashant khandalkar via DotNetMonster.com, Feb 1, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,037
    Yunus Emre ALPĂ–ZEN
    Feb 1, 2005
  4. msnews.microsoft.com
    Replies:
    6
    Views:
    3,305
    msnews.microsoft.com
    Apr 28, 2005
  5. Andrew Robinson

    Dynamic LinkButtons

    Andrew Robinson, Jun 8, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    4,906
    Walter Wang [MSFT]
    Jun 12, 2006
Loading...

Share This Page