大家好,有一个问题想请教一下:
我在页面上放了很多个用户控件,在访问的时候我做了一个函数
Sub ShowCarLocation(ByVal pg As Web.UI.Page)
        Dim C As Web.UI.Control
        For Each C In pg.Controls
            If TypeOf C Is Web.UI.UserControl Then
                If CType(C, UCCar).Pid Is Nothing Then
                    C.Visible = False
                Else
                    C.Visible = True
                End If
            End If
        Next
    End Sub其中Pid属性是用户控件的属性.
请问一下为什么总是不能判断不成功,明明我的页面上有用户控件,请指教!

解决方案 »

  1.   

    web界面上的控件组织是一棵树,须要用递归才能遍历到所有的控件
      

  2.   

    Button butSendCar1=new Button();
    foreach(DataListItem anItem in DataList1.Items)
    {
       butSendCar1 = (Button)anItem.FindControl("butSendCar1");
    }
      

  3.   

    To zjsen(星愿)(2004,浮躁的一年...) 
    能不能给出一些代码,指引一下呢
      

  4.   

    “web界面上的控件组织是一棵树,须要用递归才能遍历到所有的控件”
    就是大控件里面套小控件,一次遍历没办法找到所有的。
      

  5.   

    但是我用
     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '在此处放置初始化页的用户代码
            Dim C As Web.UI.Control
            For Each C In Page.Controls
                Dim I As String = C.GetType.ToString
            Next
        End Sub
    做了下调试,发现I的值分别是
    System.Web.UI.ResourceBasedLiteralControl
    System.Web.UI.HtmlControls.HtmlForm
    System.Web.UI.LiteralControl
    我只知道中间一个,其余不知道是什么类,如何遍历?
      

  6.   

    Sub ShowCarLocation(ByVal control As Web.UI.Control)
            Dim C As Web.UI.Control
            For Each C In control.Controls            If(C.HasControls()) Then ShowCarLocation(C)            If TypeOf C Is Web.UI.UserControl Then
                    If CType(C, UCCar).Pid Is Nothing Then
                        C.Visible = False
                    Else
                        C.Visible = True
                    End If
                End If
            Next
        End Sub
      

  7.   

    http://www.dian168.com/netcode/dtkj.htm实例,源码下载