Client side javascript: Which button caused the postback?

  • Thread starter Thread starter MAX2006
  • Start date Start date
M

MAX2006

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
 
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.type.toLowerCase() == 'submit')
{
list.oldClick = list.click;
list.click = function()
{
lastClicker = this.id;
if (this.oldClick) this.oldClick();
}
}
}


-- bruce (sqlwork.com)
 
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:
(e-mail address removed).

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

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
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
 
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)
 
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:
(e-mail address removed).
--------------------
From: "Max2006" <[email protected]>
References: <[email protected]>
 
"__EVENTTARGET" is used by control that use javascript to do a form post
(anything that is not an <input> of type image or submit).

-- bruce (sqlwork.com)
 
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:
(e-mail address removed).

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

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
 
Back
Top