2009-06-11 4 views
0

누구나 콘텐츠 형식이 속한 마스터 페이지/서브 마스터 페이지를 반복하고 해당 마스터 페이지/서브 마스터 페이지의 각 컨트롤을 반복하는 함수가 있습니까?누구든지 master page control hierachy를 반복하는 vb.net 함수를 가지고 있습니까?

내가 묻는 이유는 내가 점점 더 혼란스러워지는 마스터 페이지 스택을 만들고 컨트롤을 "잃어 버리기"시작하고있는 것입니다. 즉, 계층 구조에서 길을 잃어 버리기 때문에 찾을 수없는 것입니다.

내 방향 제어를 찾을 수 있도록 계층 구조의 트리를 덤프 할 일부 기능이 있으면 좋겠습니까.

답변

1

재귀 적 방법을 사용하면 쉽게이 작업을 수행 할 수 있습니다. 당신이 더 좋아지기를 원한다면 유연성을 높이기 위해 일부 델리게이트를 매개 변수로 사용하는 확장 메서드를 사용할 수도 있습니다. 같이 수행 할 수있는 모든 컨트롤을 통해 걸어 계층보기를 인쇄 할 페이지 내부에서이를 활용

Public Module ControlParser 
    Sub New() 
    End Sub 
    <System.Runtime.CompilerServices.Extension> _ 
    Public Sub ForEachRecursive(ByVal ctrl As Control, ByVal callback As Action(Of Control), ByVal onMethodEnter As Action, ByVal onMethodLeave As Action) 
        onMethodEnter.Invoke() 
        
        If ctrl Is Nothing Then 
            Exit Sub 
        End If 
        
        callback(ctrl) 
        
        For Each curCtrl As Control In ctrl.Controls 
            ForEachRecursive(curCtrl, callback, onMethodEnter, onMethodLeave) 
        Next 
        
        onMethodLeave.Invoke() 
    End Sub 
End Module 

: 메소드를 구현

은 App_Code 폴더에 새로운 클래스를 삭제하는 것만 큼 쉽습니다 그래서 :

Private depth As Int32 = -1 
Private sbOutput As New StringBuilder() 
Private Const SPACES_PER_TAB As Int32 = 4 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) 
    Me.ForEachRecursive(PrintControl, Function() System.Math.Max(System.Threading.Interlocked.Increment(depth),depth - 1), Function() System.Math.Max(System.Threading.Interlocked.Decrement(depth),depth + 1)) 
    //Output is a panel on the page 
    output.Controls.Add(New LiteralControl(sbOutput.ToString())) 
End Sub 

Public Sub PrintControl(ByVal c As Control) 
    sbOutput.Append(GetTabs(depth) + c.ClientID & "<br />") 
End Sub 

Public Function GetTabs(ByVal tabs As Int32) As [String] 
    Return If(tabs < 1, String.Empty, New [String]("*"c, tabs * SPACES_PER_TAB).Replace("*", "&nbsp;")) 
End Function 

코드가 재미있어 보이면 코드화하지만 C#으로 코딩하고 변환기를 사용했습니다. 그러나 실제 VB 코드를 내 App_Code 디렉토리에 덤프하여 제대로 작동하는지 확인할 수있었습니다.

희망이 도움이 :)

+0

는이 라인에 문제가 갖는 Me.ForEachRecursive (PrintControl(),() 함수를 System.Math.Max ​​(System.Threading.Interlocked.Increment (깊이), 깊이 - 1), Function() System.Math.Max ​​(System.Threading.Interlocked.Decrement (depth), depth + 1)) printControl에는 컨트롤이 필요하지만 어디에서 가져올 지 모르겠습니다 .. – NoCarrier

+0

PrintControl 뒤에 괄호를 넣었다. ForEachRecursive는 첫 번째 매개 변수로 Action 대리자를 사용합니다. 대리자를 매개 변수로 전달할 때 괄호없이 함수의 이름 만 전달합니다. 이런 방식으로 .Net은 함수에 대한 참조 만 전달하고 적절한 매개 변수를 사용하여 다시 호출합니다. 델리게이트에 대한 자세한 내용은 다음을 참조하십시오. http://www.developerfusion.com/article/5251/delegates-in-vbnet/ – Josh

관련 문제