2010-12-01 2 views
0

아래 코드를 사용하여 "MainContents"에서 각 텍스트 상자 컨트롤을 반복 할 수 있습니다. Q1 : 짧은 길이 있습니까? 나는 VB로 C 번호에서이 변환"MainContents"에서 각 텍스트 상자 컨트롤을 반복 하시겠습니까?

For Each ctrl As Control In Page.Controls 
     For Each subctrl As Control In ctrl.Controls 
      For Each subctrlsub As Control In subctrl.Controls 
       If TypeOf subctrlsub Is System.Web.UI.WebControls.ContentPlaceHolder Then 
        If subctrlsub.ClientID = "MainContent" Then 
         For Each ct As Control In subctrlsub.Controls 
          If TypeOf ct Is System.Web.UI.WebControls.TextBox Then 
           For r As Short = 1 To 8 
            For c As Short = 1 To 6 
             .... (do something) ... 
            Next 
           Next 
          End If 
         Next 
        End If 
       End If 
      Next 
     Next 
    Next 

답변

0

(? "MainContents"모든 컨트롤을 얻기 위해) 잠재적 인 부정확 용서. 이를 재귀라고합니다.

Protected Sub DoSomething(ctrl As Control) 
    For Each c As Control In ctrl.Controls 
     If TypeOf c Is ContentPlaceHolder Then 
      If c.ClientID = "MainContent" Then 
          // Do your stuff 
      End If 
     End If 

     If c.Controls.Count > 0 Then 
      DoSomething(c) 
     End If 
    Next 
End Sub 
관련 문제