how do I know it's posted back due to a button click in Form_Load

Discussion in 'ASP .Net' started by =?Utf-8?B?SmF6eg==?=, Apr 27, 2006.

  1. Hello,
    I have an asp button control(called buttonA), and in the Form_Load event I
    want to know if the postback is due to the click of ButtonA. Which method
    should I use to achieve this?

    Thanks,
    Jazz
     
    =?Utf-8?B?SmF6eg==?=, Apr 27, 2006
    #1
    1. Advertising

  2. =?Utf-8?B?SmF6eg==?=

    Paul Bush Guest

    Probably a better way but right off the top here goes.

    asp:hiddenfield, session key, querystring, etc...

    protected void _buttonA_Click(object sender, EventArgs e)
    {
    _hiddenField.Value = "A";
    // you could also do the following:
    // Session["sessionKey"] = "A";
    }


    if(_hiddenField == "A")
    {
    // do something here
    }

    if(Session["sessionKey"].ToString() == "A")
    {
    // do something here
    }
     
    Paul Bush, Apr 27, 2006
    #2
    1. Advertising

  3. Re: how do I know it's posted back due to a button click in Form_L

    Paul,
    What I want is to detect if it's from ButtonA click in the Form_Load
    even(this is before the ButtonCLick event gets called), and if it is I will
    skip some code in the Form_Load .

    Thanks,
    Jazz
     
    =?Utf-8?B?SmF6eg==?=, Apr 27, 2006
    #3
  4. Re: how do I know it's posted back due to a button click in Form_L

    What do you mean by the Form_Load event, do you have a control called Form
    and Form_Load is the event handler for it's OnLoad event?

    "Jazz" wrote:

    > Paul,
    > What I want is to detect if it's from ButtonA click in the Form_Load
    > even(this is before the ButtonCLick event gets called), and if it is I will
    > skip some code in the Form_Load .
    >
    > Thanks,
    > Jazz
     
    =?Utf-8?B?Y2xpY2tvbg==?=, Apr 28, 2006
    #4
  5. =?Utf-8?B?SmF6eg==?=

    Damien Guest

    Re: how do I know it's posted back due to a button click in Form_L

    Jazz wrote:
    > Paul,
    > What I want is to detect if it's from ButtonA click in the Form_Load
    > even(this is before the ButtonCLick event gets called), and if it is I will
    > skip some code in the Form_Load .
    >
    > Thanks,
    > Jazz


    In VB:

    If Not Request.Params("ButtonA") is Nothing Then
    'Do stuff if button A was pressed
    Else
    'Do stuff if button A wasn't pressed
    End If

    However, I wouldn't recommend it. You've now, potentially, got two
    places where code reacts to button presses - in the Page Load event and
    in the button Click event. A future maintainer (or a future you) may be
    confused if they don't spot the code in Page Load, and spend valuable
    time trying to diagnose why the page acts differently when the button
    is pressed.

    Perhaps if you could elaborate on what behaviour you're altering, we
    could suggest some viable alternatives?

    Damien
     
    Damien, Apr 28, 2006
    #5
  6. =?Utf-8?B?SmF6eg==?=

    Bill Gregg Guest

    Re: how do I know it's posted back due to a button click in Form_L

    In the Form Load event handler, can't you interrogate the sender object
    to determine whether or not button A was pressed?

    Bill


    *** Sent via Developersdex http://www.developersdex.com ***
     
    Bill Gregg, Apr 28, 2006
    #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. Paul Aspinall
    Replies:
    1
    Views:
    410
    Duray AKAR
    Aug 12, 2003
  2. jorge
    Replies:
    2
    Views:
    1,289
    jorge
    May 26, 2004
  3. =?Utf-8?B?VG9u?=
    Replies:
    2
    Views:
    555
    =?Utf-8?B?VG9u?=
    Jun 22, 2007
  4. Fresh
    Replies:
    2
    Views:
    648
    Bo Persson
    Apr 22, 2008
  5. Paul Aspinall

    Button causes Form_Load to fire, before Button_Click

    Paul Aspinall, Aug 12, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    122
    Paul Aspinall
    Aug 12, 2003
Loading...

Share This Page