요즘은 다들 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; }
'.NET > ASP.NET' 카테고리의 다른 글
Repeater 콘트롤 DataBind() 호출 시 흐름 (0) | 2014.06.02 |
---|---|
runat=server 에 의한 ID, NAME 변경 막는법 (0) | 2014.04.17 |
[펌]비하인드 코드에서 웹페이지나 웹서비스를 html로 읽어 오고 싶을때.. (0) | 2014.03.06 |
Visual Studio 2010 에서 Visual Studio 2013 로 컨버젼시 using 에러 해결 (0) | 2014.02.19 |
ASP.NET WebAPI와 Knockout.js를 활용한 SPA 개발 (0) | 2014.02.07 |