Javascript: Determine Which Control Caused Postback
Solution 1:
See:
How does
ASP.NET
recognize the control responsible for handling the postback? When no controls referenced in the request body implement theIPostBackEventHandler
interface, the page class looks for the__EVENTTARGET
hidden field, if any. The contents of the field is assumed to be theID
of the control that caused the postback. If this control implements theIPostBackEventHandler
interface, theRaisePostbackEvent
method is invoked.
this is from here - The Client Side of ASP.NET Pages.
So at the client-side __EVENTTARGET
is all you need. At the server-side you could either override the Page.RaisePostBackEvent
method (this is protected method, so you could inherit from System.Web.UI.Page
class):
protectedoverridevoidRaisePostBackEvent(IPostBackEventHandler sourceControl,
string eventArgument)
{
// sourceControl is a control that caused postbackbase.RaisePostBackEvent(sourceControl, eventArgument);
}
or perform the same without inherining:
var controlName = page.Request.Params["__EVENTTARGET"];
Control postbackControl = null;
if (!string.IsnullOrEmpty(controlName))
{
postbackControl = this.Page.FindControl(controlName);
}
EDIT: regarding the author's comment to my answer: if __EVENTTARGET
value is an empty string, it seems you're getting this value before it is been set in __doPostBack
function. So the workaround could be in overriding__doPostBack
function or a similar way; you could find an example of doing it in this SO quesion.
Solution 2:
You may be able to use the ScriptManager.GetCurrent().AsyncPostBackSourceElementID
to find the control that caused the postback to the page.
More details on MSDN but it will work in these instances:
A postback from a control that is inside an UpdatePanel control whose ChildrenAsTriggers property is set to true (the default).
A postback from a control that is a trigger for an UpdatePanel control.
A postback from a control that is registered by calling the RegisterAsyncPostBackControl method of the ScriptManager control.
I don't know if you're using UpdatePanels but it seems likely as this is the only situation I can think of where you'd need to know which control is causing the postback.
Post a Comment for "Javascript: Determine Which Control Caused Postback"