foreach(Control ctr in this.Controls)
           {
               if(ctr.GetType()==typeof(HtmlForm))
               {
                  foreach(Control ctr1 in ctr.Controls)
                  {
  // Response.Write(ctr1.ID);                     if(ctr1.GetType()==typeof(TextBox))
                     {
                        TextBox tb=(TextBox)ctr1;

    Response.Write(tb.Text.Trim());
                     }
                  }
               }
           }

解决方案 »

  1.   

    动态生成控件时设置其ID
    然后根据findcontrol来获取这样做并不能保证一定可以得到
      

  2.   

    特别情况,不能用FindControl这种方法.请问还有其它方法吗?
      

  3.   

    你的控件是在哪儿阶段动态生成的?
    page_load还是CreateChildControl
      

  4.   

    这要看你的控件是在页面生命周期的哪个阶段动态生成的?
    page_load还是CreateChildControl、CreateControlHierarchy
      

  5.   

    private void Page_Load(object sender, System.EventArgs e)
    {
       build_table();
    }
    private void build_table()
    {
    cn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
    cn.Open();
    string mysql= "SELECT FORMULA_ITEM_NAME,FORMULA_ITEM_NO FROM HR_SALARYFORMULA_DETAIL WHERE FORMULA_NO='" + FORMULA_NO +"'";
    SqlCommand cm = new SqlCommand(mysql,cn);
    SqlDataReader dr = cm.ExecuteReader(); int my_col=0;
                int i=0;

    System.Web.UI.WebControls.TableRow tr;
    tr = new TableRow() ;
    Table1.Rows.Add(tr); while(dr.Read())
    {
    if(my_col == 3)
    {
    my_col = 0;
    tr = new TableRow() ;
    Table1.Rows.Add(tr);
    } System.Web.UI.WebControls.TableCell tt;
    tt = new TableCell();
    tr.Cells.Add(tt); ls_tempid = "MyTextBox" + i+"_t";
    Label lb=new Label();
    lb.ID = ls_tempid;
    lb.Text = dr["FORMULA_ITEM_NAME"].ToString().Trim();
    lb.Visible = true;
    lb.Width =60;
    tt.Controls.Add(lb);
    //---
    System.Web.UI.WebControls.TableCell td;
    td = new TableCell();
    tr.Cells.Add(td); ls_tempid = "MyTextBox" + i; TextBox cc=new TextBox();
    cc.ID=ls_tempid;
    //cc.Text=ls_tempid;
    cc.Text=dr["FORMULA_ITEM_NAME"].ToString().Trim().Substring(0,1);
    cc.Visible=true;
    cc.Enabled=true;
    cc.Width = 60;
    td.Controls.Add(cc);
    //---
    i++;                
    my_col++;
    }
       cn.Close();
    }
      

  6.   

    需要递归调用
    因为空间是已数的方式存放的C#
    遍历页面的所有元素
    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实例,源码下载