Reading Event Objects (like a clicked button) at Page_Load

K

Krista Lemieux

Hello,

In my form I have a button, and the button_Click function
(which gets called when the button is clicked). However
the functionality that I have in the button_Click
function, needs to be in the Page_Load, since I need to
change a value based on the button click to render the
page. So my question is, can I access the button object
(the sender of the event) in the Page_Load. So that in
the Page_Load I can have the following functionality:
If Button1 is pressed then Do this
If Button2 is pressed then do that
If Button3 is pressed then do ... and so on...

Is this possible?

Thank you very much for your help.

Merci,
Krista Lemieux
 
K

Karl

Yes, but it isn't pretty...someone might already have written a control out
there to do this, but basically:

If you only have 1 button, you can assess that the button has been pressed
by the following rule:
Page.IsPostback == true and Request.Form["__EVENTTARGET"] is null

Now, this isn't your case (unfortunetly), so you have two options:

(I just thought of this one, but I likes it!)
1 - Add a javascript onClick that'll store the Id of the button being
clicked in a hidden form field, retrieve the id in Page_Load which'll let
you do a FindControl() for the button. This could be nicely wrapped in a
server control which extends the existing Button functionality.

2 - Your Restquest.Form collection will only have the value of the button
that was clicked. You can loop through it until you find the id/name of a
button, an dyou'll know that was the clicked one.

Karl
 
K

Krista Lemieux

Hi Karl,

Ohh my, I though that there would be some direct way to
do this, but from what you're telling me, there isn't...
However I do think your idea is really good, and I'll
definatelly use, since there is no direct way through
the .NET freamwork.

Thank you for your help.

Merci,
Krista Lemieux
-----Original Message-----
Yes, but it isn't pretty...someone might already have written a control out
there to do this, but basically:

If you only have 1 button, you can assess that the button has been pressed
by the following rule:
Page.IsPostback == true and Request.Form ["__EVENTTARGET"] is null

Now, this isn't your case (unfortunetly), so you have two options:

(I just thought of this one, but I likes it!)
1 - Add a javascript onClick that'll store the Id of the button being
clicked in a hidden form field, retrieve the id in Page_Load which'll let
you do a FindControl() for the button. This could be nicely wrapped in a
server control which extends the existing Button functionality.

2 - Your Restquest.Form collection will only have the value of the button
that was clicked. You can loop through it until you find the id/name of a
button, an dyou'll know that was the clicked one.

Karl

Hello,

In my form I have a button, and the button_Click function
(which gets called when the button is clicked). However
the functionality that I have in the button_Click
function, needs to be in the Page_Load, since I need to
change a value based on the button click to render the
page. So my question is, can I access the button object
(the sender of the event) in the Page_Load. So that in
the Page_Load I can have the following functionality:
If Button1 is pressed then Do this
If Button2 is pressed then do that
If Button3 is pressed then do ... and so on...

Is this possible?

Thank you very much for your help.

Merci,
Krista Lemieux


.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top