zoukankan      html  css  js  c++  java
  • asp.net清空页面上的所有TextBox

    Asp.net中一次性清空页面上的所有TextBox中的内容,由于TextBox在客户端以<input type=”text”>形式来呈现的,因此解决方案有客户端和服务器端两种方式,服务器端包括两种方法!这个破东西在asp.net面试题中广为流传(我感受颇深).

    方法一:

    Code
     1foreach (Control c in this.FindControl("form1").Controls)
     2{
     3    if (c is TextBox)
     4    {
     5        ((TextBox)c).Text = "";
     6    }
     7}
     8
     9
    10
     

    方法二:

    Code
    1 FieldInfo[] infos = GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance);
    2        for (int i = 0; i < infos.Length; i++)
    3        {
    4            if (infos[i].FieldType == typeof(TextBox))
    5            {
    6                ((TextBox)infos[i].GetValue(this)).Text = "";
    7            }
    8        }
     

    方法三:(客户端事件)

    Code
     1<script language="javascript" type="text/javascript">
     2        function ClearAllTextBox() {
     3            var obj = window.document.forms[0];
     4            for (i = 0; i < obj.elements.length; i++) {
     5                var elem = obj.elements[i];
     6                if (elem) {
     7                    if (elem.type == "text") {
     8                        elem.value = "";
     9                    }
    10                } 
    11            } 
    12        }
    13    </script>

    今天偶做测试,把通用函数粘贴如下:

    public void ClearTextBox(ControlCollection ControlArray)
            {
                foreach (Control c in ControlArray)
                {
                    if (c.Controls.Count>0)
                    {
                        ClearTextBox(c.Controls);
                    }
                    else
                    {
                        if (c is TextBox)
                        {
                            ((TextBox)c).Text = "";
                        }
                        if (c is HtmlInputText)
                        {
                            ((HtmlInputText)c).Value = "";
                        }
                    }
                }
            }

  • 相关阅读:
    Unity 3(一):简介与示例
    MongoDB以Windows Service运行
    动态SQL中变量赋值
    网站发布IIS后堆栈追踪无法获取出错的行号
    GridView Postback后出错Operation is not valid due to the current state of the object.
    Visual Studio 2010 SP1 在线安装后,找到缓存在本地的临时文件以便下次离线安装
    SQL Server 问题之 排序规则(collation)冲突
    IIS 问题集锦
    linux下安装mysql(ubuntu0.16.04.1)
    apt-get update 系列作用
  • 原文地址:https://www.cnblogs.com/Akgu/p/5145558.html
Copyright © 2011-2022 走看看