要求如下: 传一 webform 的class 名称: 要在一类(.cs)里取到这class里的所有server 端控件ID 包括htmlcontrol  and webcontrol .
例如 :
有一WebForm1 调用了一 Class Inc.cs现在要在Inc.cs 里取到 WebForm1 里的所有server 端控件ID ...请高手指点。在线等待 in ...  

解决方案 »

  1.   

    这种也不行呀foreach(System.Web.UI.Control ctl in this.Page.Controls)
    {
    string ctlID=ctl.ClientID.ToString();
    Response.Write(ctlID);
    }
      

  2.   

    取到的结果是: _ctl0
    Form1
    _ctl1
      

  3.   

    对啊。那就是控件的CLIENTID啊
      

  4.   

    findpagecontrol( Page page )
    {
        foreach( WebControl wc in page.Controls )
        {
             string id = wc.Id;
             findcontrol( wc );
        }
    }
    void  findcontrol( WebControl wc )
    {
        foreach( WebControl wcC in wc.Controls )
       {
           string strId = wcC.Id;
           findcontrol( wcC );
        }
    }
      

  5.   

    前提:webform1必须继承 Inc.cs 类Public Class webform1
        Inherits Inc先给你个vb的
    Protected Sub SetFocusStyle()
            Dim i As Integer
            Dim item As New Object
            Dim c As Panel        For i = 0 To Page.Controls.Count - 1
                For Each item In Page.Controls(i).Controls
                    If TypeOf item Is System.Web.UI.WebControls.TextBox Then
                        item.Attributes.Add("onfocus", "javascript:this.style.backgroundColor='khaki';")
                        item.Attributes.Add("onblur", "javascript:this.style.backgroundColor='';")                End If
                  
                    If TypeOf item Is System.Web.UI.WebControls.Panel Or TypeOf item Is System.Web.UI.HtmlControls.HtmlGenericControl Or TypeOf item Is System.Web.UI.HtmlControls.HtmlTableCell Then
                        Dim cItem As New Object
                        For Each cItem In item.Controls
                            If TypeOf cItem Is System.Web.UI.WebControls.TextBox Then
                                cItem.Attributes.Add("onfocus", "javascript:this.style.backgroundColor='khaki';")
                                cItem.Attributes.Add("onblur", "javascript:this.style.backgroundColor='';")
                            End If
                        Next
                    End If
                Next
            Next
        End Sub
      

  6.   

    这个是可以在当前页面取到。但在 Inc.cs 没法取呀! 
    楼下的有没人知?  foreach (Control tt in this.Controls) 

    if (tt.ID!=null) 

    foreach (Control dd in tt.Controls) 

    Response.Write(  "<br>"  );  Response.Write(dd.ID);  } 
    }  }
      

  7.   

    int i, size;
    size =ccAny.Controls.Count;
    for (i=0; i<size; i++) 

    string s = ccAny.Controls[i].ClientID;
    }
      

  8.   

    还有取到id后能否去操作改控件呢? 比如 :
     foreach (Control tt in this.Controls) 

    if (tt.ID!=null) 

    foreach (Control dd in tt.Controls) 

    Response.Write(  "<br>"  );  Response.Write(dd.ID); 
     // 加了以下内容
    if(dd 为textbox )                                        
    dd.Text="操作测试"; } 
    }  }
      

  9.   

    如果用楼上的这些访求,要求传递的page是一个实例化后当前实例,所以只能在本页面调用一个后台类的这个方法,我写了一个小例子,楼主看一下
    页面方法调用:
    System.Text.StringBuilder strBuild = new System.Text.StringBuilder();
    inc.findControl(strBuild,this.Page);
    this.Label1.Text = strBuild.ToString();后台类(inc.cs)的方法定义:
    public static void findControl(System.Text.StringBuilder strBuild,Page page)
    {
    foreach(Control ctr in page.Controls)
    {
    findControl(strBuild,ctr);
    }
    }
    public static void findControl(System.Text.StringBuilder strBuild,Control ctr)
    {
    strBuild.Append(ctr.ID).Append( "<br>");
    foreach(Control ctr1 in ctr.Controls)
    {
    findControl(strBuild,ctr1);
    }
    }
      

  10.   

    楼上的还不行呀。用page.Controls只能取到个 from 里面的东东全没有。 我上面贴出来的只可在当前页面的cs里取。到了 Inc.cs就不知如何取了。哪位高人有取控件的方法。顺路再问在Inc.cs 里如何才能更改 WebForm1 里控件的内容????????
      

  11.   

    跟据楼上各位大侠提供的方法及本人的查找的方法结合终于搞定了。代码如后面所示: 
    //////////////////////////////////////////////
    在此非常感谢楼上各位。
    呆会就结贴。可惜偶没分了。不然可多给点! 后台类(inc.cs)的方法定义:
    public string findControl(Page page)
    { string kk=null;
    foreach(Control ctr in page.Controls[1].Controls)

    kk=kk+ctr.ClientID.ToString()+"|";
    if(ctr.GetType().ToString()=="System.Web.UI.WebControls.TextBox")
    {
    ((TextBox)ctr).Text="搞定咯___________";
    }
     
    }return kk;
    }