private string getPostBackControlName()
{
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)
{
control = c;
break ;
}
}
}
if (control != null )
return control.ID;
else
return string .Empty;
}
因此要取得來源只需要如下呼叫即可
//沒有元件呼叫(如第一次載入回傳為空白),其他則回傳元件名
string button_name = getPostBackControlName();
沒有留言:
張貼留言