473,326 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Please Help! How to identify which button was clicked?

Hi all and thanks in advance,
I am stuck! I can't figure out how to identify which button was clicked on my ASP.NET page in the PostBack event? So what I am trying to do is to is to have an if statement like as follows in the PageLoad:

private void Page_Load(object sender, System.EventArgs e) {

if (!Page.IsPostBack) {

//do something here

} else {

if (btnSave.clicked)

//do something here

else

//do something else

}

I have my methods btnSave_Click and other buttons setup. It is just that when the buttons are clicked they still go through the Page load event and within the pageload event I need to differentiate which one of the buttons are clicked since it is essential to my code.

Am I crazy for wanting to do something like this? It was very easy to do before with ASP?

Thanks for your input!

Nov 18 '05 #1
7 15958
Amadelle wrote:
Hi all and thanks in advance,
I am stuck! I can't figure out how to identify which button was clicked
on my ASP.NET page in the PostBack event? So what I am trying to do is
to is to have an if statement like as follows in the PageLoad:
private void Page_Load(object sender, System.EventArgs e) {

if (!Page.IsPostBack) {

//do something here

} else {

if (btnSave.clicked)

//do something here

else

//do something else

}

I have my methods btnSave_Click and other buttons setup. It is just
that when the buttons are clicked they still go through the Page load
event and within the pageload event I need to differentiate which one of
the buttons are clicked since it is essential to my code.

Am I crazy for wanting to do something like this? It was very easy to
do before with ASP?

Thanks for your input!


I know there's a way to do this, not off-hand, but I am going to ask
'why?'? Since you have the events coded already, you'll know which
button when you get there (to one of them). Plus 'cancelling' the event
may be tough if that is something in mind (in Page_Load, if you want to
cancel or stop right there and redisplay).....

I'm curious as to why you're wanting to do this, if you can share...

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Nov 18 '05 #2
Amadelle,

If all of the buttons are of the same type then you can cast sender as the button directly like this:

Dim MyButton As Button = CType(sender, Button)

Then you can tell which button was clicked like this:

Select Case MyButton.ID
Case "MyButton1"
'---Do Something
Case "MyButton2"
'---Do Something Else
End Select

If you have different types of buttons you'll need to find out which type you're dealing with first before you may cast:

Select Case sender.GetType.ToString
Case "System.Web.UI.WebControls.ImageButton"
'---Cast to image button here
Case "System.Web.UI.WebControls.LinkButton"
'---Cast to link button here
End Select
--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Amadelle" <am******@yahoo.com> wrote in message news:O$**************@TK2MSFTNGP12.phx.gbl...
Hi all and thanks in advance,
I am stuck! I can't figure out how to identify which button was clicked on my ASP.NET page in the PostBack event? So what I am trying to do is to is to have an if statement like as follows in the PageLoad:

private void Page_Load(object sender, System.EventArgs e) {

if (!Page.IsPostBack) {

//do something here

} else {

if (btnSave.clicked)

//do something here

else

//do something else

}

I have my methods btnSave_Click and other buttons setup. It is just that when the buttons are clicked they still go through the Page load event and within the pageload event I need to differentiate which one of the buttons are clicked since it is essential to my code.

Am I crazy for wanting to do something like this? It was very easy to do before with ASP?

Thanks for your input!

Nov 18 '05 #3
"Amadelle" <am******@yahoo.com> wrote in message news:O$**************@TK2MSFTNGP12.phx.gbl...
Hi all and thanks in advance,
I am stuck! I can't figure out how to identify which button was clicked on my ASP.NET page in the PostBack event? So what I am trying to do is to is to have an if statement like as follows in the PageLoad:

The best solution I've found to this problem is to not do it.

Don't ask in Page_Load which control caused the PostBack. Instead, handle the postback event of each control:

private void btnSave_Click(object sender, EventArgs e)
{
// Action when Save clicked
}

private void btnCancel_Click(object sender, EventArgs e)
{
// Action when Cancel clicked
}
--
John Saunders
johnwsaundersiii at hotmail

Nov 18 '05 #4
I have come across this problem before. Sometimes you need to know before the event handler fires which button was clicked, particularly if you are creating dynamic controls or something that needs to be in page_load based on the button press.

The solution so far is to do a HttpContext.Current.Request[] of the buttons id. If the request comes back null, the button was not pressed. If the Request comes back with the ID of the button (essentially just a marker anyway) then it was that button that was pressed.

Example:
Page_Load:

// adding the button
Button oButton = new Button();
oButton.ID = "PRESSME1";
oCell.Controls.Add(oButton);
....
Somewhere else in PageLoad

....///testing for the button being pressed.
if (HttpContext.Current.Request["PRESSME1"] != null)
// the button was pressed. do something like add a dropdown list orsomething.
Another tricky one is trying to tell if a DropDownList or Checkbox marked as AutoPost has caused a postback without using event handlers. In this case you can use the JavaScript that .NET creates and hijack the "__event_target" form variable.
Do a request on the __EventTarget and whatever ID comes back is what caused the postback --- if the postback was caused by a DropDown or CheckBox or something that has AutoPost back. If a submit button caused the post back the __eventtarget is empty but you can do a request of the button's ID as described above.

Andrew S.
"John Saunders" wrote:
"Amadelle" <am******@yahoo.com> wrote in message news:O$**************@TK2MSFTNGP12.phx.gbl...
Hi all and thanks in advance,
I am stuck! I can't figure out how to identify which button was clicked on my ASP.NET page in the PostBack event? So what I am trying to do is to is to have an if statement like as follows in the PageLoad:

The best solution I've found to this problem is to not do it.

Don't ask in Page_Load which control caused the PostBack. Instead, handle the postback event of each control:

private void btnSave_Click(object sender, EventArgs e)
{
// Action when Save clicked
}

private void btnCancel_Click(object sender, EventArgs e)
{
// Action when Cancel clicked
}
--
John Saunders
johnwsaundersiii at hotmail

Nov 18 '05 #5
"Andy Z Smith" <An********@discussions.microsoft.com> wrote in message
news:A8**********************************@microsof t.com...
I have come across this problem before. Sometimes you need to know before the event handler fires which button was clicked, particularly if you are
creating dynamic controls or something that needs to be in page_load based
on the button press.
The solution so far is to do a HttpContext.Current.Request[] of the buttons id. If the request comes back null, the button was not pressed. If
the Request comes back with the ID of the button (essentially just a marker
anyway) then it was that button that was pressed.
Example:
Page_Load:

// adding the button
Button oButton = new Button();
oButton.ID = "PRESSME1";
oCell.Controls.Add(oButton);
...
Somewhere else in PageLoad

...///testing for the button being pressed.
if (HttpContext.Current.Request["PRESSME1"] != null)
// the button was pressed. do something like add a dropdown list orsomething.

Another tricky one is trying to tell if a DropDownList or Checkbox marked as AutoPost has caused a postback without using event handlers. In this
case you can use the JavaScript that .NET creates and hijack the
"__event_target" form variable. Do a request on the __EventTarget and whatever ID comes back is what caused the postback --- if the postback was caused by a DropDown or CheckBox
or something that has AutoPost back. If a submit button caused the post
back the __eventtarget is empty but you can do a request of the button's ID
as described above.

I've yet to find a situation where I've needed to know in Page_Load what
control posted back. It's always been possible to allow the postback events
and postback data events to fire and then to process the results of those
events in later handlers, like in the PreRender event. I have found this to
be true even for cases where the entire control hierarchy is created
dynamically.

Also, the techniques you mention are very dependant on the specific
implementation of the current version of ASP.NET. The two underscores in
"__EVENTTARGET" are an indicator that this is an internal,
implementation-dependant name, subject to change whenever they feel like it,
or just whenever they want to play with your mind...
--
John Saunders
johnwsaundersiii at hotmail
"John Saunders" wrote:
"Amadelle" <am******@yahoo.com> wrote in message news:O$**************@TK2MSFTNGP12.phx.gbl... Hi all and thanks in advance,
I am stuck! I can't figure out how to identify which button was clicked on my ASP.NET page in the PostBack event? So what I am trying to do
is to is to have an if statement like as follows in the PageLoad:
The best solution I've found to this problem is to not do it.

Don't ask in Page_Load which control caused the PostBack. Instead, handle the postback event of each control:
private void btnSave_Click(object sender, EventArgs e)
{
// Action when Save clicked
}

private void btnCancel_Click(object sender, EventArgs e)
{
// Action when Cancel clicked
}
--
John Saunders
johnwsaundersiii at hotmail

Nov 18 '05 #6
Agreed ... you should be able to do everything you need in the PreRender
handler. Use button click handlers to set page properties and/or
members, and process them on PreRender.

When I first begun .NET development, my initial instinct was to load
controls on page load as well, but that soon proved to be a pain. Since
then, I started using PreRender, and never looked back. Seems as though
you are spending the extra effort, going outside of the framework
provided, to achieve something you could have accomplished much faster
using the intrinsic event handlers and processing postback data on
Prerender.

ib.

John Saunders wrote:
"Andy Z Smith" <An********@discussions.microsoft.com> wrote in message
news:A8**********************************@microsof t.com...
I have come across this problem before. Sometimes you need to know before


the event handler fires which button was clicked, particularly if you are
creating dynamic controls or something that needs to be in page_load based
on the button press.
The solution so far is to do a HttpContext.Current.Request[] of the


buttons id. If the request comes back null, the button was not pressed. If
the Request comes back with the ID of the button (essentially just a marker
anyway) then it was that button that was pressed.
Example:
Page_Load:

// adding the button
Button oButton = new Button();
oButton.ID = "PRESSME1";
oCell.Controls.Add(oButton);
...
Somewhere else in PageLoad

...///testing for the button being pressed.
if (HttpContext.Current.Request["PRESSME1"] != null)
// the button was pressed. do something like add a dropdown list


orsomething.

Another tricky one is trying to tell if a DropDownList or Checkbox marked


as AutoPost has caused a postback without using event handlers. In this
case you can use the JavaScript that .NET creates and hijack the
"__event_target" form variable.
Do a request on the __EventTarget and whatever ID comes back is what


caused the postback --- if the postback was caused by a DropDown or CheckBox
or something that has AutoPost back. If a submit button caused the post
back the __eventtarget is empty but you can do a request of the button's ID
as described above.

I've yet to find a situation where I've needed to know in Page_Load what
control posted back. It's always been possible to allow the postback events
and postback data events to fire and then to process the results of those
events in later handlers, like in the PreRender event. I have found this to
be true even for cases where the entire control hierarchy is created
dynamically.

Also, the techniques you mention are very dependant on the specific
implementation of the current version of ASP.NET. The two underscores in
"__EVENTTARGET" are an indicator that this is an internal,
implementation-dependant name, subject to change whenever they feel like it,
or just whenever they want to play with your mind...

Nov 18 '05 #7
Whereas I agree that __EVENTTARGET is subject to change in later versions, I disagree that the _need_ to know what triggered a PostBack doesn't exist.

On a complex form, you've got to worry about stuff like that. Take the following situation: you've got a checkbox and a button in a repeater. The CheckBox can only AutoPostBack. It can not generage the repeater_ItemCommand event. On the other hand, the button fires the ItemCommand event.

Therefore, if you set up a handler for the checkbox autopostback, when you click the button that handler will also get called. If your button relies on state that the checkbox handler changes, then you've got an inconsistent state by the time the ItemCommand event gets triggered.

Therefore, the Request[buttonId] method is very good at figuring out what's going on before processing things in PageLoad. However, say button1 is your Button object, you have to use Request[button1.UniqueID] instead of Request[button1.ID] or Request[button1.ClientID]. Use UniqueID because this is what gets registered to the Request object if the button1 object is nested inside repeaters or other parent objects on your form.
Apr 21 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kurt Watson | last post by:
I’m having a different kind of problem with Hotmail when I sign in it says, "Web Browser Software Limitations Your Current Software Will Limit Your Ability to Use Hotmail You are using a web...
7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
13
by: sd00 | last post by:
Hi all, can someone give me some coding help with a problem that *should* be really simple, yet I'm struggling with. I need the difference between 2 times (Target / Actual) However, these times...
1
PEB
by: PEB | last post by:
POSTING GUIDELINES Please follow these guidelines when posting questions Post your question in a relevant forum Do NOT PM questions to individual experts - This is not fair on them and...
0
by: 2Barter.net | last post by:
newsmail@reuters.uk.ed10.net Fwd: Money for New Orleans, AL & GA Inbox Reply Reply to all Forward Print Add 2Barter.net to Contacts list Delete this message Report phishing Show original
6
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID,...
5
by: tabani | last post by:
I wrote the program and its not giving me correct answer can any one help me with that please and specify my mistake please it will be highly appreciable... The error arrives from option 'a' it asks...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.