public ControlCollection GetControls(Page page){
int controlNum = page.Controls.Count;
ControlCollection cc = this.CreateControlCollection();
for (int i = 0;i < controlNum;i++){
if (page.Controls[i].ID != null){
cc.Add(page.Controls[i]);
}
}
return cc;
}
Line 23:  if (page.Controls[i].ID != null){Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index
请问问题处在哪了?还有我这种遍历页面控件的方法是否可行?

解决方案 »

  1.   

    你的方法接受的参数是Page类型,那么你传进来的是什么参数呢?你是怎么调用这个方法的捏?
      

  2.   

    我想实现的功能是:建一个公共类,方法是GetControls(Page page),获得页面上所有的控件,包括Web控件跟Html控件,参数是某个页面,然后在需要获得控件的页面上调用这个方法,不知这种方法是否可行?
      

  3.   

    Public Function GetControls(ByVal thispage As Page) As String
            Dim strControl As String
            For maxC As Integer = 0 To thispage.Controls.Count - 1
                If Not thispage.Controls(maxC).ID Is Nothing Then
                    strControl &= thispage.Controls(maxC).GetType.Name & "<br>"
                    If thispage.Controls(maxC).HasControls Then
                        For Each minC As Control In thispage.Controls(maxC).Controls
                            If Not minC.ID Is Nothing Then
                                strControl &= minC.GetType.Name & "<br>"
                            End If
                        Next
                    End If
                End If
            Next
            Return strControl
        End Function
    这个试试!
      

  4.   

    我试了,这样获得的只是HtmlForm<Br>HtmlForm<Br>
      

  5.   

    protected System.Web.UI.WebControls.TextBox UserId;
    protected System.Web.UI.WebControls.Panel Panel1;
    protected System.Web.UI.HtmlControls.HtmlForm Form1;
    protected ProlinkControl.ProlinkTable ProlinkTable1;
    protected System.Web.UI.WebControls.TextBox Password; private string strControl; private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    Response.Write(FindControl());
    } public string FindControl(){
    for (int i = 0;i < Page.Controls.Count;i++){
    if (!(Page.Controls[i].ID == null)){
    strControl += Page.Controls.GetType().Name + "<Br>";
    foreach(System.Web.UI.Control control in Page.Controls){
    if (control.ID != null){
    strControl += control.ID + "<Br>";
    }
    }
    }
    }
    return strControl;
    }输出来的是:ControlCollection<Br>Form1<Br>
      

  6.   

    public string FindControl(){
    for (int i = 0;i < Page.Controls.Count;i++){
    if (!(Page.Controls[i].ID == null)){
    strControl += Page.Controls[i].GetType().Name + "<Br>";
    foreach(System.Web.UI.Control control in Page.Controls[i].Controls){
    if (control.ID != null){
    strControl += control.ID + "<Br>";
    }
    }
    }
    }
    return strControl;
    }
    你有两处错误,帮你改好了!