473,324 Members | 2,178 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,324 software developers and data experts.

Client side javascript: Which button caused the postback?

Hi,

I am doing some client side javascipt work. I have a handler for
window.onUnload event and within the code; I need to know the name of the
asp.net button caused the postback. How can I do that?

Thank you
Max

Sep 16 '08 #1
8 2160
there is no builtin way. one approach would to attach a handler to every
button, and log the button name. something like:

(air code - be simpler with jQuery)

var lastClicker = '';
var list = document.getElementsByTagName('input');
for (var i in list)
{
if (list[i].type.toLowerCase() == 'submit')
{
list[i].oldClick = list[i].click;
list[i].click = function()
{
lastClicker = this.id;
if (this.oldClick) this.oldClick();
}
}
}
-- bruce (sqlwork.com)
MAX2006 wrote:
Hi,

I am doing some client side javascipt work. I have a handler for
window.onUnload event and within the code; I need to know the name of
the asp.net button caused the postback. How can I do that?

Thank you
Max
Sep 16 '08 #2
Hi Max,

I think one simple approach is always manually record the id at the click
event of each button element itself. And Bruce just provided some script
which is used to register such script programmatically.

BTW, javascript also support some event properties, you can try inspect some
on it:

http://www.quirksmode.org/js/events_properties.html

However, I think in "unload" event, it maybe too late to check information
for the former "click/submit" event.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
"MAX2006" <al*******@newsgroup.nospamwrote in message
news:37**********************************@microsof t.com...
Hi,

I am doing some client side javascipt work. I have a handler for
window.onUnload event and within the code; I need to know the name of the
asp.net button caused the postback. How can I do that?

Thank you
Max

Sep 16 '08 #3
Hi Steven and Bruce,

ASP.NET has some magic way to know which button was pressed. That is why I
am almost sure there is some way in JavaScript to know which button is going
to cause a postback.

I am know that I can add javascript event handlers to buttons, but I would
like to know how ASP.NET finds which button pressed without any event
handler?

Please consider the fact that my javascript code is called before postback
within window.onUnload event.

Thank you for help,
Max
"Steven Cheng" <st*****@online.microsoft.comwrote in message
news:#O*************@TK2MSFTNGP06.phx.gbl...
Hi Max,
H
I think one simple approach is always manually record the id at the click
event of each button element itself. And Bruce just provided some script
which is used to register such script programmatically.

BTW, javascript also support some event properties, you can try inspect
some on it:

http://www.quirksmode.org/js/events_properties.html

However, I think in "unload" event, it maybe too late to check information
for the former "click/submit" event.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
"MAX2006" <al*******@newsgroup.nospamwrote in message
news:37**********************************@microsof t.com...
>Hi,

I am doing some client side javascipt work. I have a handler for
window.onUnload event and within the code; I need to know the name of the
asp.net button caused the postback. How can I do that?

Thank you
Max

Sep 16 '08 #4
there is no magic.

when the browser posts the form, its sends the button name and value of the
button that caused the postback in the post data. javascript has no access to
the actual post data, only the orignal form elements.

-- bruce (sqlwork.com)
"Max2006" wrote:
Hi Steven and Bruce,

ASP.NET has some magic way to know which button was pressed. That is why I
am almost sure there is some way in JavaScript to know which button is going
to cause a postback.

I am know that I can add javascript event handlers to buttons, but I would
like to know how ASP.NET finds which button pressed without any event
handler?

Please consider the fact that my javascript code is called before postback
within window.onUnload event.

Thank you for help,
Max
"Steven Cheng" <st*****@online.microsoft.comwrote in message
news:#O*************@TK2MSFTNGP06.phx.gbl...
Hi Max,
H
I think one simple approach is always manually record the id at the click
event of each button element itself. And Bruce just provided some script
which is used to register such script programmatically.

BTW, javascript also support some event properties, you can try inspect
some on it:

http://www.quirksmode.org/js/events_properties.html

However, I think in "unload" event, it maybe too late to check information
for the former "click/submit" event.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
"MAX2006" <al*******@newsgroup.nospamwrote in message
news:37**********************************@microsof t.com...
Hi,

I am doing some client side javascipt work. I have a handler for
window.onUnload event and within the code; I need to know the name of the
asp.net button caused the postback. How can I do that?

Thank you
Max
Sep 16 '08 #5
"Max2006" <al*******@newsgroup.nospamwrote in message
news:44**********************************@microsof t.com...
ASP.NET has some magic way to know which button was pressed.
No magic is involved.
I would like to know how ASP.NET finds which button was pressed
By inspecting the __EVENTTARGET hidden field in the Request.Form collection.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Sep 16 '08 #6
Hi Max,

As Bruce said, the current ASP.NET (2.0 or later) simply via the button
control's "value" property which is posted as http request content to
detect which control cause the postback. However, at client-side script, we
have no means to detect such http request value yet. In former ASP.NET
version, it use a hidden field named "__EVENTTARGET", however, it seems
this is no longer used in new version.

Sincerely,

Steven Cheng
Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.
--------------------
>From: "Max2006" <al*******@newsgroup.nospam>
References: <37**********************************@microsoft.co m>
<#O*************@TK2MSFTNGP06.phx.gbl>
>Subject: Re: Client side javascript: Which button caused the postback?
Date: Tue, 16 Sep 2008 10:44:50 -0400
>
Hi Steven and Bruce,

ASP.NET has some magic way to know which button was pressed. That is why I
am almost sure there is some way in JavaScript to know which button is
going
>to cause a postback.

I am know that I can add javascript event handlers to buttons, but I would
like to know how ASP.NET finds which button pressed without any event
handler?

Please consider the fact that my javascript code is called before postback
within window.onUnload event.

Thank you for help,
Max
"
Sep 17 '08 #7
"__EVENTTARGET" is used by control that use javascript to do a form post
(anything that is not an <inputof type image or submit).

-- bruce (sqlwork.com)

Steven Cheng wrote:
Hi Max,

As Bruce said, the current ASP.NET (2.0 or later) simply via the button
control's "value" property which is posted as http request content to
detect which control cause the postback. However, at client-side script, we
have no means to detect such http request value yet. In former ASP.NET
version, it use a hidden field named "__EVENTTARGET", however, it seems
this is no longer used in new version.

Sincerely,

Steven Cheng
Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.
--------------------
>From: "Max2006" <al*******@newsgroup.nospam>
References: <37**********************************@microsoft.co m>
<#O*************@TK2MSFTNGP06.phx.gbl>
>Subject: Re: Client side javascript: Which button caused the postback?
Date: Tue, 16 Sep 2008 10:44:50 -0400
>Hi Steven and Bruce,

ASP.NET has some magic way to know which button was pressed. That is why I
am almost sure there is some way in JavaScript to know which button is
going
>to cause a postback.

I am know that I can add javascript event handlers to buttons, but I would
like to know how ASP.NET finds which button pressed without any event
handler?

Please consider the fact that my javascript code is called before postback
within window.onUnload event.

Thank you for help,
Max
"
Sep 17 '08 #8
Thanks for the input Bruce,

Yep, therefore, this element is not reliable since not every page will
guranteed to have this field rendered.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#notifications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>Date: Tue, 16 Sep 2008 20:04:57 -0700
From: bruce barker <no****@nospam.com>
Organization: SQLWork.com
User-Agent: Thunderbird 2.0.0.16 (Macintosh/20080707)
MIME-Version: 1.0
Subject: Re: Client side javascript: Which button caused the postback?

"__EVENTTARGET" is used by control that use javascript to do a form post
(anything that is not an <inputof type image or submit).

-- bruce (sqlwork.com)

Steven Cheng wrote:
>Hi Max,

As Bruce said, the current ASP.NET (2.0 or later) simply via the button
control's "value" property which is posted as http request content to
detect which control cause the postback. However, at client-side script,
we
>have no means to detect such http request value yet. In former ASP.NET
version, it use a hidden field named "__EVENTTARGET", however, it seems
this is no longer used in new version.

Sincerely,

Steven Cheng
Microsoft MSDN Online Support Lead
Sep 17 '08 #9

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

Similar topics

1
by: Mike Moore | last post by:
Can an asp.net button that is a server side control call a client side java function when the button is clicked? If so, please provide example.....thanks.,
1
by: Christopher D. Wiederspan | last post by:
I'm wondering if there's a way to use javascript to disable a ASP.NET web page before it starts to postback. Specifically, here's what I'm running into. I've got a webform that has an autopostback...
6
by: Valerian John | last post by:
I have a ListBox webcontrol on an aspx page. Items are added to the ListBox using client-side code. However, when the page is posted back the items are missing/not available. (It is like the...
4
by: Guadala Harry | last post by:
Suppose I have a hyperlink that, when clicked, executes a JavaScript function on the client. Separately I have a button that, when clicked, causes a post back and executes a server-side function,...
6
by: Guadala Harry | last post by:
I have some client-side JavaScript that, among other things, calculates the value of a variable (myVar). On the server I have a stored procedure that needs to somehow receive as an input parameter...
10
by: Ben | last post by:
Hi, I made an application in classic asp (reservation of books and video stuffs for students) and want to migrate to asp.net. The user has to chose a date, then pushung on a submit button. The...
5
by: Bjorn Sagbakken | last post by:
Hello I have just migrated from VS 2003 to VS 2005, and .NET framework 1.1 to 2.0 I am at the end of debugging and fixing stuff. Now there is one error I just cannot find a solution to: On...
24
by: =?Utf-8?B?cGF0cmlja2RyZA==?= | last post by:
Hi everyone! I have a hidden input field in a form which I change in some occasions on the client using javascript, but when I use "view source" I can't see these changes reflected on the page!...
4
by: Lewis Holmes | last post by:
Hi I have the following situation in one of my asp.net pages. The user can add multiple table rows to a form by selecting a button. These rows can contain asp.net controls. When this button is...
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.