Wire up a click event within another click event doesn't work...

Discussion in 'ASP .Net' started by Chu, Apr 10, 2006.

  1. Chu

    Chu Guest

    Thanks everyone for taking a moment to read this.

    I've got a page where I use a LinkButton and I wire up a dynamic event
    to the button. When the user clicks the button, the event is fired as
    expected. In the event code for that button, a new LinkButton is added
    to the page and is wired up to yet a different event, however when
    clicked, the page is posted back but the event is not triggered.

    I'm assuming it has something to do with the order of events that are
    being fired (i.e, it appears page_load gets hit then the first
    LinkButton's click event is fired) and so the wire up isn't working out
    as expected.

    My problem is that I have no idea where to go from here :) Is it even
    possible to do what I'm wanting, i.e, wire up a 2nd button on the same
    page that the 1st button was pushed?

    Thanks for any help.
     
    Chu, Apr 10, 2006
    #1
    1. Advertising

  2. It isn't an easy problem to solve.

    Basically, for an event to fire, it needs to be hooked up on postback during
    or before page_load.

    you already have 2 problems.

    (a) you aren't hooking it up during postback (well, you are during 1
    postback, but not the 2nd one which is the important one)
    (b) even if you were, it'd be too late

    this is what you are doing:

    Button 1 clicked
    Page_Load
    Setup event for button1 (you might be doing this somewhere else, you
    didn't specify
    end load
    LoadPostback
    Create 2nd button
    Setup eventhandler for button2
    end loadPostback

    Button2 Clicked
    Page_Load
    Nothing's happening
    end load
    No events to raise


    What needs to happen in Button2 click is:
    Page_Load
    Recreate button2
    Hook up the event
    end load
    Here your event will fire.


    As I said, it isn't fun to solve. If your pages don't need viewstate, I'd
    personally use straight links and pass vlaues in the querystring <a
    href="samepage.aspx?action=button2">blah</a>

    If viewstate is important, you need to do somethin glike:

    Button1 click:
    create2nd button
    ViewState.Add("Load2ndButton", true);
    end click

    Postback:
    Page_Load
    IF Page.IsPostBack then
    If ViewState["Load2ndButton"] = true then
    Load button 2
    Setup event handler
    end if
    end if
    End page load


    all pseudo code of course...hopefully that gives you some ideas..

    Karl

    --
    http://www.openmymind.net/



    "Chu" <> wrote in message
    news:...
    > Thanks everyone for taking a moment to read this.
    >
    > I've got a page where I use a LinkButton and I wire up a dynamic event
    > to the button. When the user clicks the button, the event is fired as
    > expected. In the event code for that button, a new LinkButton is added
    > to the page and is wired up to yet a different event, however when
    > clicked, the page is posted back but the event is not triggered.
    >
    > I'm assuming it has something to do with the order of events that are
    > being fired (i.e, it appears page_load gets hit then the first
    > LinkButton's click event is fired) and so the wire up isn't working out
    > as expected.
    >
    > My problem is that I have no idea where to go from here :) Is it even
    > possible to do what I'm wanting, i.e, wire up a 2nd button on the same
    > page that the 1st button was pushed?
    >
    > Thanks for any help.
    >
     
    Karl Seguin [MVP], Apr 10, 2006
    #2
    1. Advertising

  3. Chu

    Winista Guest

    Its the dynamically added link button that is causing the problem for you.
    By the time page_load finishes the control hierarchy should be recreated so
    that the events can be wired properly to the control that caused it. Since
    you are not adding that link button in Page_Load or before, that event is
    lost.
    What you may have to do is, look at __EVENTTARGET value in FORM's collection
    in page_load to check if the event was caused by your dynamically added
    control and then do your stuff.

    http://www.netomatix.com

    "Chu" <> wrote in message
    news:...
    > Thanks everyone for taking a moment to read this.
    >
    > I've got a page where I use a LinkButton and I wire up a dynamic event
    > to the button. When the user clicks the button, the event is fired as
    > expected. In the event code for that button, a new LinkButton is added
    > to the page and is wired up to yet a different event, however when
    > clicked, the page is posted back but the event is not triggered.
    >
    > I'm assuming it has something to do with the order of events that are
    > being fired (i.e, it appears page_load gets hit then the first
    > LinkButton's click event is fired) and so the wire up isn't working out
    > as expected.
    >
    > My problem is that I have no idea where to go from here :) Is it even
    > possible to do what I'm wanting, i.e, wire up a 2nd button on the same
    > page that the 1st button was pushed?
    >
    > Thanks for any help.
    >
     
    Winista, Apr 10, 2006
    #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. Derrick

    wire enter to button click

    Derrick, Jun 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    335
    Derrick
    Jul 2, 2004
  2. Alex
    Replies:
    2
    Views:
    1,970
  3. sonic
    Replies:
    0
    Views:
    515
    sonic
    Jun 26, 2006
  4. Russ
    Replies:
    2
    Views:
    440
  5. quoclinh
    Replies:
    4
    Views:
    144
    quoclinh
    Jun 26, 2007
Loading...

Share This Page