我们有一些遗留代码需要在Page_Load中识别哪个事件导致了回发.目前,这是通过检查这样的请求数据来实现的......
if(Request.Form ["__ EVENTTARGET"]!= null
&&(Request.Form ["__ EVENTTARGET"].IndexOf("BaseGrid")> -1 // BaseGrid事件(例如排序)
|| Request.Form ["btnSave" ]!= null //保存按钮
如果有人重命名控件,这非常难看并且会中断.有没有更好的方法呢?
重写每个页面,以便它不需要在Page_Load中检查它,目前不是一个选项.
这应该让你获得导致回发的控件:
public static Control GetPostBackControl(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;
}
}
}
return control;
}
Run Code Online (Sandbox Code Playgroud)
在此页面上阅读更多相关信息:http: //ryanfarley.com/blog/archive/2005/03/11/1886.aspx
归档时间: |
|
查看次数: |
7222 次 |
最近记录: |