본문으로 바로가기

요즘은 다들 MVC가 대세라 웹폼 을 잘 쓰진 않지만..유지보수를 위해 아직도 필요하다..

아래 예제에서 GetType()을 이용하여 원하는 콘트롤만 체크가능하게 한다면 유용하게 써먹을 수 있을거 같다.

출처 : http://stackoverflow.com/questions/3018280/asp-net-during-page-load-how-can-i-get-the-id-of-the-control-that-submitted For anyone who might interested in this (at least what worked for me). Cen provided the answer. In your Page_Load event add:

Control c= GetPostBackControl(this.Page); if(c != null) { if (c.Id == "btnSearch") { SetFocus(txtSearch); } } Then in your basepage code add: 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) //해당 체크 부분을 GetType()과 함께 파라메타 인자를 한개 더 받아서 비교하면 다양한 콘트롤 비교 가능! { control = c; break; } } } return control; }