Loading

net下遍历内容页所有控件实现重置功能

在C#中没有<Form>里自带的"reset"命令按钮,因此需要自己写代码实现重置功能。包括文本框清空,复选框清空等操作。

通常情况下,遍历页面中的所有控件,将TextBox的值设为空即可,代码如下:

               foreach (Control control in this.Controls)
                {
                    if (control is TextBox)
                    {
                        ((TextBox)control).Text = "";
                    }
                    if (control is ListBox)
                    {
                       ((ListBox)control).Items.Clear();
                    }
                }

 

但是在内容页中,this.Controls却不包含页面上的控件,因此遍历时并不能得到所需的控件集合。

原因不再赘述。解决方法是通过Master.FindControl获取当前内容页的ContentPlaceHolder,再遍历其子控件。

代码如下:

      ContentPlaceHolder ContentID = (ContentPlaceHolder)     Master.FindControl("ContentID ");
        foreach (Control control in ContentID .Controls)
        {
            if (control is TextBox)
            {
                ((TextBox)control).Text = "";
            }
            if (control is CheckBox)
            {
                ((CheckBox)control).Checked = false;
            }
        }

 

posted @ 2012-09-02 01:18  The Mechanic  阅读(194)  评论(0编辑  收藏  举报