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

G

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
 
P

Paul Bush

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
}
 
G

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
 
G

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?
 
D

Damien

Jazz said:
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
 
B

Bill Gregg

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

Bill
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top