2010-04-22 2 views
0

asp.net 사용자 정의 컨트롤 인 userControl1.ascx와 다른 사용자 정의 컨트롤 인 userControl2.ascx가 있습니다. userControl2는 userControl1 안에 있습니다. userControl1은 UpdatePanel 컨트롤 안에 있습니다. userControl2에 버튼이 있으면 밀어 넣을 때 일반 게시물을 다시 갖고 싶습니다. ScriptManager.RegisterPostBackControl (버튼)을 사용하고 싶습니다. 마스터 페이지에 ScriptManager가 있습니다. Page_Load 이벤트에 단추를 등록하려면 userControl2에서 ScriptManager에 액세스하는 방법을 모르겠습니다. 그래서, 어떻게해야합니까?UserControl 내의 사용자 정의 컨트롤에있는 ScriptManager

답변

1

재귀 FindControl 메서드를 사용하여 스크립트 관리자를 찾을 수 있습니다. 이것은 최선의 방법은 아니지만 일을 끝내게됩니다. 이 작업을 수행하는 데는 꽤 좋은 방법은 아닙니다.

var scriptManager = FindControl(Page, "IdOfScriptManager"); 

public static Control FindControlRecursive(Control root, string id) 
    { 
     if (root.ID == id) 
     { 
      return root; 
     } 

     foreach (Control c in root.Controls) 
     { 
      Control t = FindControlRecursive(c, id); 
      if (t != null) 
      { 
       return t; 
      } 
     } 

     return null; 
    } 
+0

일반 찾기 제어가 여기에서 작동하지 않는 이유를 말해 줄 수 있습니까? – user204588

+0

ScriptManager는 Page의 직접적인 하위 항목이 아닐 수도 있습니다. 다른 컨트롤 안에있을 수 있습니다. 마스터 페이지와 콘텐츠 자리 표시 자도 컨트롤로 간주되므로 마스터 페이지를 사용하는 경우가 일반적입니다. – ntziolis

+1

ScriptManager.GetCurrent가 좋지 않습니까? http://stackoverflow.com/questions/8601525/how-to-get-the-scriptmanager-placed-on-master-page-into-child-pages-code-behind –