在Page_Load期间,我想捕获执行回发的控件.
protected void Page_Load(object sender,EventArgs e) { if (!Page.IsPostBack) { } // Capture the control ID here. }
像往常一样,任何想法将不胜感激!
解决方法
对于任何可能对此感兴趣的人(至少对我有用的东西).
Cen提供了答案.
在您的Page_Load事件中添加:
Control c= GetPostBackControl(this.Page); if(c != null) { if (c.Id == "btnSearch") { SetFocus(txtSearch); } }
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; }
你可以看到原始的帖子here
希望这可以帮助.