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 = "";
                    }
                }
            }
        }

asp.net清空页面上的所有TextBox的更多相关文章

  1. Web页面上的控件

    Web页面,即:.aspx文件页面的根目录下,分为了5部分 [0]-{System.Web.UI.LiteralControl} [1]-{System.Web.UI.HtmlControls.Htm ...

  2. asp.net遍历页面中所有TextBox,并赋值为String.Empty的方法

    本文介绍下,如何用.net遍历页面中的所有TextBox控件,并赋值为string.empty的方法,通过实例学习具体操作.有需要的朋友可以参考下.   一.遍历窗体控件 1,普通页面遍历TextBo ...

  3. asp.net 输出 页面内容 在服务器上

    .定义页面内容 按 Ctrl+C 复制代码 <asp:Content ID="BodyContent" runat="server" ContentPla ...

  4. asp.net 页面上的点击事件

    asp.net 页面上 服务器端控件Button 有两个click事件如 <asp:Button ID="Button1" runat="server" ...

  5. 在asp.net页面上按回车会触发Imagebutton控件的Click事件

    原文:在asp.net页面上按回车会触发Imagebutton控件的Click事件 问题: 用asp.net做的aspx页面,无论是否有文本框.下拉框.复选框……获得焦点,只要在当前页面上按一下回车就 ...

  6. ASP.NET Web API 帮助(help)页面上没有 Test API按钮的解决方法

    参与一个web API项目时发现它的help页面特别好用,不仅可以根据webapi的方法和注释自动生成帮助文档以方便查阅,还可以在这个页面上测试webapi方法.于是在自己新建项目时也打算将这个hel ...

  7. [AaronYang原创] 大话ASP.NET MVC3+ (C#与DOM与JS页面上的很炫的技巧)

    案例一:比如我要传递一个新闻标题和新闻内容 存储到数据库去 1.最简单的通用Url公式就是 function getUrl() { var d = $("#表单ID").seria ...

  8. ASP.NET 给作为隐藏域的TextBox赋值之后提交表单,无响应?

    操作步骤: 给页面隐藏TextBox赋值,然后触发ASP.NET change事件,调用ASP.NET后台方法,调用后执行客户端脚本this.RegisterClientScriptBlock(Dat ...

  9. asp.net(C#)页面事件顺序

    asp.net(C#)页面事件顺序 http://www.cnblogs.com/henw/archive/2012/02/09/2343994.html   1 using System.Data; ...

随机推荐

  1. Neural Task Programming: Learning to Generalize Across Hierarchical Tasks

    Neural Task Programming: Learning to Generalize Across Hierarchical Tasks

  2. squid代理缓存服务器

    参考文章 http://www.cnblogs.com/mchina/p/3812190.html ;

  3. Grunt学习笔记【6】---- grunt-contrib-requirejs插件详解

    本文主要讲如何使用Grunt实现RequireJS文件压缩. 一 说明 ES6出来前,RequireJS是JavaScript模块化最常用的方式之一.对于使用RequireJS构建的项目,要实现打包压 ...

  4. java参数传递------真心是值传递

    未完待续 不同意的请尽管去深入看一下. 对象是引用传递没错,参数传递是值传递.

  5. Java for LeetCode 138 Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  6. Automator 实例:使用快捷键 实现 快速在当前路径 打开 iTerm

    1. 在 finder -> 应用程序 或 通过 Spotlight 打开:Automator.app 2. 选择新建 “服务” 3. 设置服务,见下图,设置完成之后,command + s 保 ...

  7. c#应用程序带参数运行

    有时候我们需要让软件带参数运行,使用参数控制软件的部分行为, C#默认窗口应用是不带参数的,不过在Main函数的参数手动加上就可以得到参数了. 举例如下: /// <summary> // ...

  8. MVC+Ext.net零基础学习记录(四)

    在上一篇文章[MVC+Ext.net零基础学习记录(三)]中提到了利用MVC的Area可以做到项目分离,但是实际操作起来还是有很多问题的.比如,对于物理资源的访问,会报:没有相关资源 开始的时候,我在 ...

  9. codeforces 112B Petya and Square

    B. Petya and Square time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  10. js/jq 动态添加的元素不能触发绑定事件解决方案

    <!-- Copyright 2017-10-27, Jachin QQ: 381558301 Email: 381558301@qq.com 请看看你们的版本并对号入座: jquery1.6版 ...