首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

疑难算法有关问题,遍历全部控件

2013-08-11 
疑难算法问题,遍历全部控件 public void SetReadOnly(){foreach (Control c in this.Page.Controls){forea

疑难算法问题,遍历全部控件


 public void SetReadOnly()
        {
            foreach (Control c in this.Page.Controls)
            {
                foreach (Control cc in c.Controls)
                {
                    if (cc is HtmlTableRow)
                    {
                        foreach (Control ccc in cc.Controls)
                        {
                            if (cc is TextBox)
                            {
                                if ((cc as TextBox).ReadOnly == true)
                                {
                                    (cc as TextBox).ReadOnly = false;
                                    (cc as TextBox).Attributes.Add("readonly", "readonly");
                                }
                            }
                        }


                    }
                    if (cc is TextBox)
                    {
                        if ((cc as TextBox).ReadOnly == true)
                        {
                            (cc as TextBox).ReadOnly = false;
                            (cc as TextBox).Attributes.Add("readonly", "readonly");
                        }
                    }
                }
            }
        }



就出在HtmlTable这里,如果往下我不确定还有多少类似的Table控件时如何遍历。例如有一个TR标签加了runat="Server",在这个TR标签下又有一个Table.因为这样遍历是以控件为单位的,因此如果在Table或以下TR,TD便签添加了服务器runat server就会停止这层以下的遍历 算法 控件遍历 ?树结构
[解决办法]
要改成递归的写法。


        public void SetReadOnly(Control p)
        {
            foreach (Control cc in p.Controls)
            {
                if (cc is TextBox)


                {
                    if ((cc as TextBox).ReadOnly == true)
                    {
                        (cc as TextBox).ReadOnly = false;
                        (cc as TextBox).Attributes.Add("readonly", "readonly");
                    }
                }
                else
                {
                    SetReadOnly(cc);
                }
            }
        }

热点排行