许多时候我们需要获取引发回发事件的服务器控件ID,以进行不同的处理.
以下方法或者可以帮到你:
/// <summary >
/// 获取回发的控件ID by wyf
/// </summary >
/// <param name="Page" ></param >
/// <returns ></returns >
protected string getPostBackControlName(Page Page)
{
Control control = null;
string ctrlname = Page.Request.Params["__EVENTTARGET"];
if (ctrlname != null && ctrlname != String.Empty)
{
control = Page.FindControl(ctrlname);
}
else
{
Control c;
foreach (string ctl in Page.Request.Form)
{
if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
{
c = Page.FindControl(ctl.Substring(0, ctl.Length - 2));
}
else
{
c = Page.FindControl(ctl);
}
if (c is System.Web.UI.WebControls.Button ||
c is System.Web.UI.WebControls.ImageButton || c is System.Web.UI.WebControls.LinkButton)
{
control = c;
break;
}
}
}
if (control != null)
return control.ID;
else
return string.Empty;
}
a