批量清空TextBox控件的内容

时间:2022-11-26 05:49:04

由于要做的页面需要录入的数据较多,于是写了这个批量清空TextBox内容的工具类

 using System;
using System.Collections.Generic;
using System.Text;

using System.Web.UI;
using System.Web.UI.WebControls;

namespace UestcHouse.Common
{
    /// <summary>
    /// 用于批量清除页面控件内容
    /// </summary>
    public class UIClear
    {
        /// <summary>
        /// 批量清空页面TextBox内容
        /// </summary>
        /// <param name="control">页面TextBox控件的容器</param>
        public static void ClearTextBox(Control control)
        {
            ControlCollection con = control.Controls;
            for (int i = 0; i < con.Count; i++)
            {
                if (con[i].GetType().Name == "TextBox")
                {
                    ((TextBox)con[i]).Text = "";
                }
            }
        }
        /// <summary>
        /// 批量清空页面TextBox内容(可指定开始位置)
        /// </summary>
        /// <param name="control">页面TextBox控件的容器</param>
        /// <param name="start">指定开始位置</param>
        public static void ClearTextBox(Control control, int start)
        {
            ControlCollection con = control.Controls;
            for (int i = start; i < con.Count; i++)
            {
                if (con[i].GetType().Name == "TextBox")
                {
                    ((TextBox)con[i]).Text = "";
                }
            }
        }
        /// <summary>
        /// 批量清空页面TextBox内容(可指定开始位置和清空个数)
        /// </summary>
        /// <param name="control">页面TextBox控件的容器</param>
        /// <param name="start">指定开始位置</param>
        /// <param name="num">清空个数</param>
        public static void ClearTextBox(Control control, int start, int num)
        {
            ControlCollection con = control.Controls;
            int conNum = (con.Count < start + num) ? con.Count : start + num;
            for (int i = 0; i < conNum; i++)
            {
                if (con[i].GetType().Name == "TextBox")
                {
                    ((TextBox)con[i]).Text = "";
                }
            }
        }
    }
}

 

后台调用该方法

    protected void btReset_Click(object sender, EventArgs e)
    {

        Control control = this.Controls[0].Controls[3].Controls[9];  //(0)master (3)form (9) content
        UIClear.ClearTextBox(control);
    }