foreach (Control control in page.Controls)
{
 GetControls(control, language);
}public static void GetControls(Control control,string language)
{
if (control.HasControls())
            {
                foreach (Control childc in control.Controls)
                {
                    GetControls(childc, language);
                }
   }}
再问一个问题:我在遍历获取页面所有控件时,不能获取UpdatePanel里面的控件。如果不用UpdatePanel就都能可以获取到,
请问有没有好的解决方法呀?怎样或UpdatePanel里面的控件?
 
  读到UpdatePanel时control.HasControls() 判断为false,所以.....

解决方案 »

  1.   

            <asp:UpdatePanel ID="updatePanel1" runat="server">
                <ContentTemplate>
    提交刷新用的.
      

  2.   

    UpdatePanel控件是一个容器控件,好比GridView一样,你只能通过遍历GridView的Rows[].Cells[]去遍历它一个单元格里面所有的控件。
    因此如果你想遍历UpdatePanel控件里面的控件,先遍历到UpdatePanel自己,然后可以调用它的FindControl()方法通过Id名称来找到指定的控件,或者通过Controls[]属性,给下标索引的方式去找或者遍历控件。
      

  3.   

    我把代码贴全给大家看吧,主要目的是为了翻译页面所有显示文字的控件。public static void Translate(Page page, string language)
            {            foreach (Control control in page.Controls)
                {
                    GetControls(control, language);
                }
            }
            public static void GetControls(Control control,string language)
            {
                string oldText = "";
                common.Logger.LogNote("NamingContainer:" + control.NamingContainer.ToString());
                common.Logger.LogNote("ID:" + control.ID);            if (control.ID == "updatePanel1")
                {
                    newv.common.Logger.LogNote("ID:" + control.ID);
                }
                Type controlType = control.GetType();           
                switch (controlType.ToString())
                {
                    case "System.Web.UI.WebControls.TextBox":
                        oldText = ((TextBox)control).Text.ToString();
                        if (!string.IsNullOrEmpty(oldText))
                            ((TextBox)control).Text = Translate(oldText, language, oldText);
                        break;
                    case "System.Web.UI.WebControls.Label":
                        oldText = ((Label)control).Text.ToString();
                        if (!string.IsNullOrEmpty(oldText))
                            ((Label)control).Text = Translate(oldText, language, oldText);
                        break;
                    case "System.Web.UI.WebControls.Literal":
                        oldText = ((Literal)control).Text.ToString();
                        if (!string.IsNullOrEmpty(oldText))
                            ((Literal)control).Text = Translate(oldText, language, oldText);
                        common.Logger.LogNote("Literal:" + oldText.ToString());
                        break;
                    case "System.Web.UI.WebControls.LiteralControl":
                        oldText = ((LiteralControl)control).Text.ToString();
                        if (!string.IsNullOrEmpty(oldText))
                            ((LiteralControl)control).Text = Translate(oldText, language, oldText);
                        common.Logger.LogNote("LiteralControl:" + oldText.ToString());
                        break;
                    case "System.Web.UI.HtmlControls.HtmlInputButton":
                        oldText = ((HtmlInputButton)control).Value.ToString();
                        if (!string.IsNullOrEmpty(oldText))
                            ((HtmlInputButton)control).Value = Translate(oldText, language, oldText);
                        common.Logger.LogNote("HtmlInputButton:" + oldText.ToString());
                        break;
                         
                    default:
                        common.Logger.LogNote("default:" + controlType.ToString());
                        break;
                }            if (control.HasControls())
                {
                    foreach (Control childc in control.Controls)
                    {
                        GetControls(childc, language);
                    }
                }
            }
      

  4.   

    看到ContentTemplate就应该想到为什么得不到UpdatePanel里面的控件了。
    如果让你用上面的方法遍历控件的时候得到GridView中的控件你可以做到吗?显然不能。
    同样的道理,UpdatePanel控件和GridView控件时类似的,而不是和panel类似的。
    也就是要先找到UpdatePanel的ContentTemplate,然后用HasControls判断,因为控件是在ContentTemplate模板中的,而不是在UpdatePanel中的
    ----------
    UpdatePanel我没有实际用过,以上只是推测。
    ----------
    参考:
    ContentTemplate 属性包含呈现时显示在 UpdatePanel 控件内部的内容。可以以声明方式或编程方式创建模板的内容。UpdatePanel 控件的内容模板是单实例模板。因此,无需使用 FindControl 方法就可以直接在页上访问模板中所包含控件的属性。有关更多信息,请参见 TemplateInstanceAttribute 类的概述。 若要以声明方式将项添加到 UpdatePanel 控件中,请在 UpdatePanel 控件的开始标记和结束标记之间添加 <ContentTemplate> 元素。在 <ContentTemplate> 元素标记内部,声明其他控件和 HTML 元素。 
      

  5.   

    ctwei86,大哥。control.Controls.Count=0 所以control.Controls[0]这个方法的是取不到的。control.FindControl("ctl03"),但是找到的不对!并且不可每个去指定id呀得通过类型去找。yfqvip,大哥。
    ((System.Web.UI.UpdatePanel)(control)).ContentTemplate
    找到下面的方法怎么写呢?还是没取出来。能否具体一下
      

  6.   

    楼主不好意思,GirdView里面直接使用名称.Rows[].Cells[].Controls属性或者FindControl()方法,但是UpdatePannel里面还有个ContentTemplate,所以应该是这样的UpdatePanel(控件名).ContentTemplateContainer.Controls或者UpdatePanel(控件名).ContentTemplateContainer.FindControl()。
    下面是我遍历我的一个Id为UpdatePannel1的UpdatePannel得控件的例子,如果里面有TextBox,我让它里面显示"Hello!"。foreach (Control c in UpdatePanel1.ContentTemplateContainer.FindControl.Controls)
    {
          if (c is System.Web.UI.WebControls.TextBox)
          {
                ((System.Web.UI.WebControls.TextBox)c).Text = "Hello!";
          }
    }这下应该没错了,希望对楼主有帮助!
      

  7.   

    不好意思更正下代码,刚刚粘错了。foreach (Control c in UpdatePanel1.ContentTemplateContainer.Controls)
    {
         if (c is System.Web.UI.WebControls.TextBox)
         {
               ((System.Web.UI.WebControls.TextBox)c).Text = "Hello!";
         }
    }
      

  8.   

    ctwei86,大哥,在页面上这样处理确实没问题!但是,我用this传入函数中,提示以下出错: 有没有好的处理方法啊?
    出错信息:Cannot instantiate the ContentTemplate in the Init event when the ContentTemplateContainer was already created manually in UpdatePanel with ID 'updatePanel1'.
    出错跟踪信息:   at System.Web.UI.UpdatePanel.CreateContents(Boolean recreate)
       at System.Web.UI.UpdatePanel.OnInit(EventArgs e)
       at System.Web.UI.Control.InitRecursive(Control namingContainer)
       at System.Web.UI.Control.InitRecursive(Control namingContainer)
       at System.Web.UI.Control.InitRecursive(Control namingContainer)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
                try
                {
                    if (control.GetType() == typeof(UpdatePanel))
                    {
                        int i = 0;
                        foreach (Control c in ((UpdatePanel)control).ContentTemplateContainer.Controls)
                        {
                            if (c is System.Web.UI.WebControls.Literal)
                            {
                                oldText = ((System.Web.UI.WebControls.Literal)c).Text;
                                newv.common.Logger.LogNote("Literal:" + oldText.ToString());
                            }
                            i++;
                            newv.common.Logger.LogNote("UpdatePanel:" + i.ToString());
                        }                    
                    }
      

  9.   

    this传入函数?有点不解,莫非你的意思是你要别的类里面操作一个页面的控件吗?要么你把需求再说清楚点?要做什么?
      

  10.   

    怎么遍历一个Panel里所有的 checkBox?