473,394 Members | 1,218 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,394 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 2164
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.