Dynamically click button

Discussion in 'ASP .Net' started by smay, Apr 2, 2008.

  1. smay

    smay Guest

    I'm trying to raise the click event of a button from my Page_Load event so
    that after the Page_Load completes the myBtn_Click event is fired, just as if
    a user had clicked the button. I can't just call myBtn_Click, page_load must
    complete first just as it would if a user had clicked. I keep getting an
    error telling me to set the page's EnableEventValidation to false, but I
    don't want to do that. For testing, I did set it to false and used
    "DirectCast(myBtn, IPostBackEventHandler).RaisePostBackEvent(Nothing)" to
    raise the click event, but myBtn_Click was called immediately, not after
    Page_Load.

    Can anyone help with a specific VB example?
     
    smay, Apr 2, 2008
    #1
    1. Advertising

  2. smay

    David Wier Guest

    I'd suggest putting the entire action you have inside the click event into
    its own subroutine -
    Then, in the click event of the button, just call that subroutine

    That way, you can call the routine in anyway you'd like from anywhere
    (dynamically or otherwise)

    David Wier
    http://aspnet101.com
    http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with no
    bloated markup


    "smay" <> wrote in message
    news:...
    > I'm trying to raise the click event of a button from my Page_Load event so
    > that after the Page_Load completes the myBtn_Click event is fired, just as
    > if
    > a user had clicked the button. I can't just call myBtn_Click, page_load
    > must
    > complete first just as it would if a user had clicked. I keep getting an
    > error telling me to set the page's EnableEventValidation to false, but I
    > don't want to do that. For testing, I did set it to false and used
    > "DirectCast(myBtn, IPostBackEventHandler).RaisePostBackEvent(Nothing)" to
    > raise the click event, but myBtn_Click was called immediately, not after
    > Page_Load.
    >
    > Can anyone help with a specific VB example?
     
    David Wier, Apr 2, 2008
    #2
    1. Advertising

  3. smay

    Patrice Guest

    You could have this code in its own routine so that your handlers (or
    whetver place you want) can just call the real sttuff (rather than
    "embedding" this code).

    Else you could use LoadComplete if you really need to have this done after
    page_load (not even at the end of this event).

    As usual knowing a bit more what you are trying to do (and why you need to
    fake a user click) could raise more focused suggestions...

    --
    Patrice

    "smay" <> a écrit dans le message de news:
    ...
    > I'm trying to raise the click event of a button from my Page_Load event so
    > that after the Page_Load completes the myBtn_Click event is fired, just as
    > if
    > a user had clicked the button. I can't just call myBtn_Click, page_load
    > must
    > complete first just as it would if a user had clicked. I keep getting an
    > error telling me to set the page's EnableEventValidation to false, but I
    > don't want to do that. For testing, I did set it to false and used
    > "DirectCast(myBtn, IPostBackEventHandler).RaisePostBackEvent(Nothing)" to
    > raise the click event, but myBtn_Click was called immediately, not after
    > Page_Load.
    >
    > Can anyone help with a specific VB example?
     
    Patrice, Apr 2, 2008
    #3
  4. smay

    smay Guest

    Problem is the button is not visible to the user. It was just my way of
    forcing code to run after the Page_Load completes. Any suggestions on a
    better/different way?

    "David Wier" wrote:

    > I'd suggest putting the entire action you have inside the click event into
    > its own subroutine -
    > Then, in the click event of the button, just call that subroutine
    >
    > That way, you can call the routine in anyway you'd like from anywhere
    > (dynamically or otherwise)
    >
    > David Wier
    > http://aspnet101.com
    > http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with no
    > bloated markup
    >
    >
    > "smay" <> wrote in message
    > news:...
    > > I'm trying to raise the click event of a button from my Page_Load event so
    > > that after the Page_Load completes the myBtn_Click event is fired, just as
    > > if
    > > a user had clicked the button. I can't just call myBtn_Click, page_load
    > > must
    > > complete first just as it would if a user had clicked. I keep getting an
    > > error telling me to set the page's EnableEventValidation to false, but I
    > > don't want to do that. For testing, I did set it to false and used
    > > "DirectCast(myBtn, IPostBackEventHandler).RaisePostBackEvent(Nothing)" to
    > > raise the click event, but myBtn_Click was called immediately, not after
    > > Page_Load.
    > >
    > > Can anyone help with a specific VB example?

    >
    >
    >
     
    smay, Apr 2, 2008
    #4
  5. smay

    Patrice Guest

    LoadComplete event ? You have tons of other events (includign on existing
    controls) that could be perhaps usefull ???

    What does this code ?

    By design events are called by the infrastructure so making this a button
    doesn't change the problem if this is *you* anyway that has to call this
    code rather than letting the ASP.NET infrastructure call it when needed

    --
    Patrice

    "smay" <> a écrit dans le message de news:
    ...
    > Problem is the button is not visible to the user. It was just my way of
    > forcing code to run after the Page_Load completes. Any suggestions on a
    > better/different way?
    >
    > "David Wier" wrote:
    >
    >> I'd suggest putting the entire action you have inside the click event
    >> into
    >> its own subroutine -
    >> Then, in the click event of the button, just call that subroutine
    >>
    >> That way, you can call the routine in anyway you'd like from anywhere
    >> (dynamically or otherwise)
    >>
    >> David Wier
    >> http://aspnet101.com
    >> http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with
    >> no
    >> bloated markup
    >>
    >>
    >> "smay" <> wrote in message
    >> news:...
    >> > I'm trying to raise the click event of a button from my Page_Load event
    >> > so
    >> > that after the Page_Load completes the myBtn_Click event is fired, just
    >> > as
    >> > if
    >> > a user had clicked the button. I can't just call myBtn_Click,
    >> > page_load
    >> > must
    >> > complete first just as it would if a user had clicked. I keep getting
    >> > an
    >> > error telling me to set the page's EnableEventValidation to false, but
    >> > I
    >> > don't want to do that. For testing, I did set it to false and used
    >> > "DirectCast(myBtn, IPostBackEventHandler).RaisePostBackEvent(Nothing)"
    >> > to
    >> > raise the click event, but myBtn_Click was called immediately, not
    >> > after
    >> > Page_Load.
    >> >
    >> > Can anyone help with a specific VB example?

    >>
    >>
    >>
     
    Patrice, Apr 2, 2008
    #5
  6. smay

    David Wier Guest

    As Patrice said - you can call the routine I suggested in either the
    LoadComplete or PreRender events


    David Wier
    http://aspnet101.com
    http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with no
    bloated markup


    "smay" <> wrote in message
    news:...
    > Problem is the button is not visible to the user. It was just my way of
    > forcing code to run after the Page_Load completes. Any suggestions on a
    > better/different way?
    >
    > "David Wier" wrote:
    >
    >> I'd suggest putting the entire action you have inside the click event
    >> into
    >> its own subroutine -
    >> Then, in the click event of the button, just call that subroutine
    >>
    >> That way, you can call the routine in anyway you'd like from anywhere
    >> (dynamically or otherwise)
    >>
    >> David Wier
    >> http://aspnet101.com
    >> http://iWritePro.com - One click PDF, convert .doc/.rtf/.txt to HTML with
    >> no
    >> bloated markup
    >>
    >>
    >> "smay" <> wrote in message
    >> news:...
    >> > I'm trying to raise the click event of a button from my Page_Load event
    >> > so
    >> > that after the Page_Load completes the myBtn_Click event is fired, just
    >> > as
    >> > if
    >> > a user had clicked the button. I can't just call myBtn_Click,
    >> > page_load
    >> > must
    >> > complete first just as it would if a user had clicked. I keep getting
    >> > an
    >> > error telling me to set the page's EnableEventValidation to false, but
    >> > I
    >> > don't want to do that. For testing, I did set it to false and used
    >> > "DirectCast(myBtn, IPostBackEventHandler).RaisePostBackEvent(Nothing)"
    >> > to
    >> > raise the click event, but myBtn_Click was called immediately, not
    >> > after
    >> > Page_Load.
    >> >
    >> > Can anyone help with a specific VB example?

    >>
    >>
    >>
     
    David Wier, Apr 2, 2008
    #6
    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. Purvi T
    Replies:
    0
    Views:
    864
    Purvi T
    Oct 19, 2004
  2. mike parr
    Replies:
    1
    Views:
    480
    Marina
    May 20, 2004
  3. Ben Fidge
    Replies:
    5
    Views:
    740
    S. Justin Gengo
    Jun 15, 2004
  4. Amy
    Replies:
    0
    Views:
    997
  5. Max2006
    Replies:
    2
    Views:
    1,585
    Walter Wang [MSFT]
    Sep 19, 2007
Loading...

Share This Page