在WinForm,Asp.Net中控件遍历清空

在WinForm中遍历代码:

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Control control in Controls)
            {
                if (control is CheckBox)
                {
                    ((CheckBox)control).Checked = false;
                }
                if (control is TextBox)
                {
                    ((TextBox)control).Text = string.Empty;
                }
            }
        }

在Asp.Net控件遍历有两种方法:

        protected void Button3_Click(object sender, EventArgs e)
        {
            ClearControls(this.Controls);
        }
        private void ClearControls(ControlCollection controls)
        {
            foreach (Control control in controls)
            {
                if (control.HasControls())
                {
                    ClearControls(control.Controls);
                }
                if (control is TextBox)
                {
                    ((TextBox)control).Text = string.Empty;
                }
                if (control is CheckBox)
                {
                    ((CheckBox)control).Checked = false;
                }
            }
        }

第二种是通过JavaScript方法:

 function ClearControls() {
            var obj = window.document.forms[0];
            for (i = 0; i < obj.elements.length; i++) {
                var element = obj.elements[i];
                if (element) {
                    if (element.type == "text") {
                        element.value = "";
                    }
                }
            }
        }
posted @ 2011-03-07 04:38  Jon.Zhiwei@hotmail.com  Views(297)  Comments(0)    收藏  举报