473,599 Members | 3,118 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 2178
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
1431
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
2347
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
6499
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
3118
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
3319
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
2030
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
1995
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
6633
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
4470
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
7904
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8398
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...
0
8400
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8051
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
8267
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
6725
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...
0
5438
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
3898
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1505
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.