我定义了一个类,但使用时总是出错。
  #region 得到页面中所有指定类型的Control
              /// <summary>
        /// 使用完方法后可从这个对象得到返回的控件集合
        /// </summary>
        public ArrayList AlControls;
        /// <summary>
        /// 得到页面中所有指定类型的Control
        /// </summary>
        /// <param name="pagecontrols">使用形如this.Controls返回的对象</param>
        public void ReturnControls(System.Web.UI.ControlCollection pagecontrols, string stringControlType )
        {
            if (!string.IsNullOrEmpty(stringControlType)&&pagecontrols.Count>0)
            {
                foreach(Control ct in pagecontrols)
                {
                    if (ct.GetType().ToString() == stringControlType)
                    {
                        this.AlControls.Add(ct);
                    }
                    else
                    {
                       
                            if (ct.Controls != null && ct.Controls.Count > 0)
                            {
                                ReturnControls(ct.Controls, stringControlType);
                            }
                       
                    }
                }
            }
        }
        #endregion

解决方案 »

  1.   

    this.Page.Controls[i].FindControl("XXX");
      

  2.   

    前台js不是有document.getElementsByTagName么
      

  3.   

    我试了下 如果不是TextBox强转后为null,那这种方法也可以找到你要的所有为TextBox的控件  foreach (Control c in this.div1.Controls)
            {
                TextBox t = c as TextBox;
                if (t != null)
                {
                    string id = t.ID;
                }
            }
      

  4.   

    需要递归查找才行,否则都是不对的。方法设计:
    Control[] GetControls(string controlName){}
      

  5.   


            //想封装到库里的话,自己所这个this.Page对象作参数丢过去,它代表什么我不再多说
            foreach (Control con in this.Page.Controls)
            {
                if (con.GetType() = typeof(LinkButton))
                {
                    //如果控件是LinkButton
                }
            }
      

  6.   

    需要递归查找才行,否则都是不对的。方法设计:
    Control[] GetControls(string controlName){}
    -------------------------------------------
    能说一下你这个怎么具体实现么?过程中一些东西我不是很懂
      

  7.   

    我写了一个,如果你能够给100分的话,我就给你。得到给定的控件下的所有某种类型的子控件。
    public static Control[] GetControls(Control c, string controlName)
      

  8.   

    比如text1 text2 text 3...................
    for(int i=0 ;i<4 i++)
    {
    this.form1.findcontrol("text"+i.tostring()).enabled=false;
    }
      

  9.   

    为什么要以字符串做为参数?这样你在传参的时候还要保证字符串正确,为什么不用Type作参数?
      

  10.   


            List<System.Web.UI.Control> GetControls(System.Web.UI.ControlCollection ctrls, Type t)
            {
                List<System.Web.UI.Control> list = new List<System.Web.UI.Control>();
                foreach (System.Web.UI.Control ctrl in ctrls)
                {
                    if (ctrl.GetType() == t)
                    {
                        list.Add(ctrl);
                    }
                    if (ctrl.Controls.Count > 0)
                    {
                        foreach (System.Web.UI.Control c in ctrl.Controls)
                        {
                            list.AddRange(GetControls(c.Controls, t));
                        }
                    }
                }
                return list;
            }
      

  11.   

     List<System.Web.UI.Control> GetControls(System.Web.UI.ControlCollection ctrls, Type t)
            {
                List<System.Web.UI.Control> list = new List<System.Web.UI.Control>();
                foreach (System.Web.UI.Control ctrl in ctrls)
                {
                    if (ctrl.GetType() == t)
                    {
                        list.Add(ctrl);
                    }
                    if (ctrl.Controls.Count > 0)
                    {
                        foreach (System.Web.UI.Control c in ctrl.Controls)
                        {
                            list.AddRange(GetControls(c.Controls, t));
                        }
                    }
                }
                return list;
            }        protected void Button1_Click(object sender, EventArgs e)
            {
                List<System.Web.UI.Control> list = GetControls(this.Page.Controls, TextBox1.GetType());
            }楼上的,我按上面的调用了,跟踪list,发现始终为0。也就是无效。
      

  12.   

    你看看你的TextBox是放置到哪里了,如果是直接放到form里,那应该是:
    List<System.Web.UI.Control> list = GetControls(this.Page.Form.Controls, typeof(TextBox));
    不过话说,从发帖到现在已经过了快一个月了,看来你对这个问题也不是很重视嘛
      

  13.   

    不好意思,代码确实有点问题,修改如下:        List<System.Web.UI.Control> GetControls(System.Web.UI.ControlCollection ctrls, Type t)
            {
                List<System.Web.UI.Control> list = new List<System.Web.UI.Control>();
                foreach (System.Web.UI.Control ctrl in ctrls)
                {
                    if (ctrl.GetType() == t)
                        list.Add(ctrl);
                    if (ctrl.Controls.Count > 0)
                        list.AddRange(GetControls(ctrl.Controls, t));
                }
                return list;
            }
      

  14.   

     id 不一样Type一样的啊
      

  15.   

    参考这个看看是怎样取得页面的Hyperlink,然后动态改变它的url值的:
    http://www.cnblogs.com/insus/articles/2033414.html
      

  16.   

    /// <summary>
    ///得到给定的控件下的所有某种类型的子控件。
    /// </summary>
    /// <param name="c">给定的控件。</param>
    /// <param name="controlName">要获取的子控件类型名称。</param>
    /// <returns>找到的所有子控件。</returns>
    public static Control[] GetControls(Control c, string controlName)
    {
    List<Control> controls = new List<Control>();
    BuildControls(c, controlName, controls);
    Control[] controlArray = controls.ToArray();
    return controlArray;
    } /// <summary>
    /// 构建GetControls方法中创建的controls泛型集合,为其添加元素。
    /// </summary>
    /// <param name="c">给定的控件。</param>
    /// <param name="controlName">要获取的子控件类型名称</param>
    /// <param name="controls">GetControls方法中创建的controls泛型集合的一个引用。</param>
    private static void BuildControls(Control c, string controlName, List<Control> controls)
    {
    for (int i = 0; i < c.Controls.Count; i++)
    {
    if (c.Controls[i].GetType().Name == controlName)
    controls.Add(c.Controls[i]);
    if (c.Controls[i].HasControls())
    BuildControls(c.Controls[i], controlName, controls);
    }
    }
      

  17.   

    使用方法,获取页面上的所有TextBox,返回一个Control类型的数组:
    GetControls(this.Page,"TextBox");
    获取div1下面的所有Button,返回一个Control类型的数组:
    GetControls(div1,"Button");