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

Discussion in 'ASP .Net' started by Guest, Apr 27, 2006.

  1. Guest

    Guest Guest

    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
     
    Guest, Apr 27, 2006
    #1
    1. Advertisements

  2. Guest

    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. Advertisements

  3. Guest

    Guest Guest

    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
     
    Guest, Apr 27, 2006
    #3
  4. Guest

    Guest Guest

    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?
     
    Guest, Apr 28, 2006
    #4
  5. Guest

    Damien Guest

    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. Guest

    Bill Gregg Guest

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

    Bill
     
    Bill Gregg, Apr 28, 2006
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.