asp.net清空某一类控件或置某一状态

一个一个控件的清空非常麻烦,所以写了一个方法将一类的控件清空: 

protected void btnClear_Click(object sender, EventArgs e)
    {
        foreach (Control ctl in this.Controls)
        {
            this.txtClear(ctl);
        }
    }

    private void txtClear(Control ctls)
    {
        if(ctls.HasControls())
        {
            foreach (Control ctl in ctls.Controls)
            {
                txtClear(ctl);
            }
        }
        else
        {
            if (ctls.GetType().Name == "TextBox")
            {
                TextBox tb = new TextBox();
                tb = (TextBox)this.FindControl(ctls.ID);
                tb.Text = "";
            }
            else if (ctls.GetType().Name == "DropDownList")
            {
                DropDownList Ddlist = new DropDownList();
                Ddlist = (DropDownList)this.FindControl(ctls.ID);
                Ddlist .SelectedIndex = 0;
            }
        }
    }

如果大家有其它什么好的方法,希望可以拿出来分享

共同交流 共同学习

原文地址:https://www.cnblogs.com/ZHF/p/1327866.html