Get ASP.NET control which fired a postback within a AJAX UpdatePanel
Related to this question: On postback, how can I check which control cause postback in Page_Init event
If the control is wrapped in an ASP.NET AJAX UpdatePanel, the variable "control" is empty because it has a different ID after the AJAX PostBack. Is there a solution to get the control which fired a postback within an ASP.NET Ajax UpdatePanel?
public static string GetPostBackControlName( Page page ) {
Control control = null;
/**
* First we will check the "__EVENTTARGET" because if the postback is made
* by controls which used the _doPostBack function, it will be available in the Request.Form collection.
*/
string ctrlname = page.Request.Params["__EVENTTARGET"];
if ( !String.IsNullOrEmpty( ctrlname ) ) {
control = page.FindControl( ctrlname );
} else {
/**
* If __EVENTTARGER is null, the control is a button-type and
* need to iterate over the form collection to find it.
*/
Control c = null;
string ctrlStr = null;
foreach ( string ctl in page.Request.Form ) {
if ( ctl.EndsWith( ".x" ) || ctl.EndsWith( ".y" ) ) {
/**
* ImageButtons have an additional "quasi-property" in their ID which ide开发者_JS百科ntifies
* the mouse-coordinates (X and Y).
*/
ctrlStr = ctl.Substring( 0, ctl.Length - 2 );
c = page.FindControl( ctrlStr );
} else {
c = page.FindControl( ctl );
}
if ( c is Button || c is ImageButton ) {
control = c;
break;
}
}
}
if ( control != null ) {
return control.ID;
}
return string.Empty;
}
Try the following method:
public string GetAsyncPostBackControlID()
{
string smUniqueId = ScriptManager.GetCurrent(Page).UniqueID;
string smFieldValue = Request.Form[smUniqueId];
if (!String.IsNullOrEmpty(smFieldValue) && smFieldValue.Contains('|'))
{
return smFieldValue.Split('|')[1];
}
return String.Empty;
}
The above method uses the hidden field of the ScriptManager on page. Its value can be accessed on the server by searching for a form key with the UniqueID of the ScriptManager. The value in the hidden field is in the format [UpdatePanel UniqueID]|[Postback Control ID]
. Knowing this information we can retrieve the ID of the control that initiated the asynchronous postback. And it works for submit buttons too.
Here's @bugventure 's code in VB.NET... With the modification of adding a Page reference as a parameter so I could use it in a common library.
Public Function GetAsyncPostBackControlID(ByRef page As Page) As String
Dim smUniqueId As String = ScriptManager.GetCurrent(page).UniqueID
Dim smFieldValue As String = page.Request.Form(smUniqueId)
If Not String.IsNullOrEmpty(smFieldValue) AndAlso smFieldValue.Contains("|") Then
Return smFieldValue.Split("|")(1)
End If
Return String.Empty
End Function
Usage:
' Call from a control or Page
Dim postbackControlID As String = GetAsyncPostBackControlID(Page)
' Call from a page
Dim postbackControlID As String = GetAsyncPostBackControlID(Me)
To get the control that caused the async postback, you can use the following instead of parsing
var scriptManager = ScriptManager.GetCurrent(Page);
var asyncPostBackSourceControl = Page.FindControl(scriptManager.AsyncPostBackSourceElementID);
精彩评论