473,385 Members | 2,180 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,385 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 15975
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.