Wednesday, September 22, 2010

Get the Control ID that Raised PostBack in CodeBehind file of ASP.Net

Control c = GetPostBackControl(Page);
if ((c != null))
{
if (c.ID.ToString() == "grdTemplate" || c.ID.ToString() == "btnAddTemplate")
{
// Do code whatever you want to action while clicked this button.
}
}

////////////////////////Method to get the Postback button Id///////////////
private Control GetPostBackControl(System.Web.UI.Page page)
{
Control control = null;

string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if ((ctrlname != null) & ctrlname != string.Empty)
{
control = page.FindControl(ctrlname);
}
else
{
foreach (string ctl in page.Request.Form)
{
Control c = page.FindControl(ctl);
if (c is System.Web.UI.WebControls.Button)
{
control = c;
break; // TODO: might not be correct. Was : Exit For
}
}
}
return control;
}


For More Information Click Me

No comments:

Post a Comment