473,883 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2192
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.getEle mentsByTagName( 'input');
for (var i in list)
{
if (list[i].type.toLowerCa se() == '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 programmaticall y.

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****@microsof t.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*******@news group.nospamwro te in message
news:37******** *************** ***********@mic rosoft.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.comw rote in message
news:#O******** *****@TK2MSFTNG P06.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 programmaticall y.

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****@microsof t.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*******@news group.nospamwro te in message
news:37******** *************** ***********@mic rosoft.com...
>Hi,

I am doing some client side javascipt work. I have a handler for
window.onUnloa d 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.comw rote in message
news:#O******** *****@TK2MSFTNG P06.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 programmaticall y.

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****@microsof t.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*******@news group.nospamwro te in message
news:37******** *************** ***********@mic rosoft.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*******@news group.nospamwro te in message
news:44******** *************** ***********@mic rosoft.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****@microsof t.com.
--------------------
>From: "Max2006" <al*******@news group.nospam>
References: <37************ *************** *******@microso ft.com>
<#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
"__EVENTTAR GET" 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****@microsof t.com.
--------------------
>From: "Max2006" <al*******@news group.nospam>
References: <37************ *************** *******@microso ft.com>
<#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****@microsof t.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
1444
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
2375
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 dropdown box, and a couple of textboxes just below it. When the user selects an option from the dropdown, it triggers a postback event, which in-turn slightly adjusts the remainder of the page. The problem is that if the user is on a slow...
6
6514
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 page does not know the items were added client-side.) TIA, Val
4
3127
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, as in the following: <a id="HyperLink5" href="javascript:DoSomethingInClient()">Do Something</a> <asp:Button id="btnSaveChanges" EnableViewState="true" CommandName="SaveChanges" Visible="true" Width="115" runat="server" Text="Save...
6
3332
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 the value of myVar. I need to know how to get the value of myVar from the script up to the server. Specifically, I'd like to have the following happen when the user clicks a button in the browser: 1. The client-side script executes (and...
10
2063
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 whole day is then displayed in cels of a table. The user has then to click in a cel representing a hour of the day and an object (book ..), and finally click on the submit button to insert that reservation in the database. My problem is: there...
5
2013
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 some pages I have applied a small client-script to trap the enter-key beeing pressed and re-route this action. With the new version this little script still works, it traps the enter-key. BUT it causes postback-errors on many other .NET control,...
24
6662
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! Does anyone know what I should do in order for the page to know the change? (Tried with enableviewstate=false, true, whatever, didn't work) I would like to have the change reflected in the page without doing a
4
4496
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 selected, the row is added using JavaScript. The script uses cloneNode to clone a hidden template row and all of its children and then adds the new row to the table, updates all of the child control Ids and sets visibility etc. The hidden...
0
11142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10847
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10416
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9574
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7971
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7129
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4220
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.