In your Page_Load event try
Control c= GetPostBackControl(this.Page); if(c != null) { if (c.Id == "btnSearch") { SetFocus(txtSearch); } }
Then add this to your page or BasePage or something else
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; }
source share