1/22/2011

How to find post back element id in asynchronous postback ( UpdatePanel ) ? ASP.NET

A day before I was playing with some functionality where I had to get the id of the element which caused the postback. If its a normal postback done with some control. We can retrieve the postback element with the code below.

string controlID = Page.Request.Params["__EVENTTARGET"];

If we use a UpdatePanel on the page, and if its a asynchronous postback with some control inside the Content template of Update panel, you will observe that EVENTTARGET parameter will be blank. So How to get the postback element id in asynchronous postback caused by Updatepanel in ASP.NET ?

Please use the function below to get the Async postback element id.

///
/// This function retrieves the ID of the control that caused the asynchronous postback.
///

///
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;
}


Hope it helps.

Love ASP.NET tips ? For more Subscribe here or click here to get updates via email

3 comments:

thanks for the information, this is one of the most wanted information as freelancer, you know is not easy to be someone without education, but with all the will to work.

I will try some loops with this. The postback can be at times a risk when hacking pertains to retriever several keyput ids.

Post a Comment