2011-10-24 4 views
0

내 SharePoint 페이지에있는 SPDataSource 컨트롤을 찾으려고합니다. 아마 잘 작동하는 다음 코드를 발견했습니다. 단지 무엇을 전달해야할지 모르겠습니다. 나는 더 많은 것을 가지고있는 것처럼SharePoint 페이지에서 컨트롤 찾기

public static Control FindControlRecursive(Control Root, string Id) 
{ 
    if (Root.ID == Id) 
     return Root; 

    foreach (Control Ctl in Root.Controls) 
    { 
     Control FoundCtl = FindControlRecursive(Ctl, Id); 

     if (FoundCtl != null) 
      return FoundCtl; 
    } 

    return null; 
} 

나는 그것이 전체 페이지를 검색하거나 컨트롤에있는 아주 최소한의 ContentPlaceHolder가하는 방법을 모르겠어요.

편집

이 보이는 여기에 기본적인 문제가있다. 설명하는 방법을 모르지만 코드를 실행하기 전에 페이지를 열지는 않습니다. 다음을 통해 사이트를 여는 중입니다 :

using (SPWeb web = thisSite.Site.OpenWeb("/siteurl/,true)) 

그래서 아래의 페이지를 찾으려고 할 때 객체 참조가 객체의 인스턴스로 설정되지 않습니다.

var page = HttpContext.Current.Handler as Page; 

은 아마도 내가 이것에 대해 잘못된 길로 갈거야, 그래서 난 그냥 가지 물건을 파악 함께 비틀 거리고있어 여기 내 초기 단계에있어!

+0

케어처럼 전화를 시도? –

+0

템플릿에서 사이트를 만드는 목록에 대한 이벤트 처리기가 있습니다. 이제 템플릿 사이트의 SPDataSource를 목록 항목에서 가져 오는 데이터로 업데이트하려고합니다. 주로 데이터를 필터링 할 수 있도록 SPDataSource의 SelectCommand를 업데이트하려고합니다. – Mike

답변

1

실제로 SharePoint가 아닌 것은 C# asp.net입니다.

어쨌든, 당신은 당신이

var myElement = (TextBox)FindControlRecursive(control, "yourelement"); 
// or 
var myElement = FindControlRecursive(control, "yourelement") as TextBox; 

같은 방법을 쓸 수 그러나보다 효율적인 방법이 있습니다뿐만 아니라 수익을 캐스팅해야이

var page = HttpContext.Current.Handler as Page; 
var control = page; // or put the element you know exist that omit (is a parent) of the element you want to find 
var myElement = FindControlRecursive(control, "yourelement"); 

대부분의 경우처럼 부를 수있는, 여기에 하나의 간단한 예가 있습니다

public static Control FindControlRecursive(string id) 
{ 
    var page = HttpContext.Current.Handler as Page; 
    return FindControlRecursive(page, id); 
} 

public static Control FindControlRecursive(Control root, string id) 
{ 
    return root.ID == id ? root : (from Control c in root.Controls select FindControlRecursive(c, id)).FirstOrDefault(t => t != null); 
} 

앞에서 설명한 것과 같은 방식으로 호출하십시오.

큰 페이지를 처리하는 경우 위의 방법이 약간 느릴 수 있으므로 대신 제네릭을 사용하는 방법을 사용하는 것이 좋습니다. 그것들은 전통적인 방법보다 훨씬 빠릅니다.

이 하나

public static T FindControlRecursive<T>(Control control, string controlID) where T : Control 
{ 
    // Find the control. 
    if (control != null) 
    { 
     Control foundControl = control.FindControl(controlID); 
     if (foundControl != null) 
     { 
      // Return the Control 
      return foundControl as T; 
     } 
     // Continue the search 
     foreach (Control c in control.Controls) 
     { 
      foundControl = FindControlRecursive<T>(c, controlID); 
      if (foundControl != null) 
      { 
       // Return the Control 
       return foundControl as T; 
      } 
     } 
    } 
    return null; 
} 

당신은 왜 당신이 코드를 통해 SPDataSource을 찾기 위해 노력하는 설명이

var mytextBox = FindControlRecursive<TextBox>(Page, "mytextBox"); 
+0

내 게시물을 수정했습니다. 코드를 작성하지 못하게 더 큰 문제가 있다고 생각합니다. – Mike