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

一、遍历窗体控件
1,普通页面遍历TextBox控件清空的方法

复制代码 代码示例:
foreach(Control c in this.controls)
{
  if(c is TextBox)
    {
           TextBox tb=(TextBox)c;
        tb.Text=String.empty; 
    }
}
//或
foreach (Control col in this.Controls) 

     if (col.GetType().Name.Equals("TextBox")) 
     { 
         ((TextBox)col).Text = String.empty;
     } 

二、遍历Asp.net页面

复制代码 代码示例:

//不含母板页
foreach (System.Web.UI.Control txtobj in this.Page.Controls)
 
{
    if (txtobj.GetType().Name .Equals("TextBox"))
    {
       // ((TextBox)txtobj).Text = String.Empty;//这是第一种方法赋值,第二种在下面
        TextBox tb = new TextBox();
         tb = (TextBox)this.FindControl(txtobj.ID);
 
         tb.Text = String.Empty;
    }
}

包含母板页
//套用母版页的页面遍历TextBox控件的方法,其他控件类似
 
foreach (Control cp in Page.Controls)  
{  
       foreach (System.Web.UI.Control ct in cp.Controls)  
       { 
            if (ct is HtmlForm)  
            {  
                  foreach (Control con in ct.Controls) 
                  {  
                      foreach (Control c in con.Controls) 
                      { 
                          if (c is TextBox) 
                          { 
                              (c as TextBox).Text = String.Empty; 
                          } 
                      }  
                  }  
             }  
       }  
}

三、清除控件内容

复制代码 代码示例:
// 清空指定页面上所有的控件内容
//public static void ClearAllContent()
//清空指定页面上所有的控件内容
//包括TextBox,CheckBox,CheckBoxList,RadioButton,RadioButtonList。不过不清
//除如ListBox,DropDownList,因为这样的控件值对当前页面来说还能用,一般这些控件里都是保存的字典数据。
//<param name="page"> 指定的页面</param>
public static void ClearAllContent(System.Web.UI.Control page)
{
int nPageControls = page.Controls.Count;
for (int i = 0; i < nPageControls; i++)
{
foreach (System.Web.UI.Control control in page.Controls[i].Controls)
{
if (control.HasControls())
{
ClearAllText(control);
}
else
{
if (control is TextBox)
(control as TextBox).Text = "";
 
if (control is CheckBox)
(control as CheckBox).Checked = false;
 
if (control is RadioButtonList)
(control as RadioButtonList).SelectedIndex = -1;
 
if (control is RadioButton)
(control as RadioButton).Checked = false;
 
if (control is CheckBoxList)
{
foreach (ListItem item in (control as CheckBoxList).Items)
{
item.Selected = false;
}
}
}//if..else
}//foreach
}//for
}

附,如何在ASP.NET下遍历指定页面上所有控件

复制代码 代码示例:

#region 清空指定页面上所有的控件内容,public static void ClearAllContent()
/// <summary>
/// 清空指定页面上所有的控件内容,包括TextBox,CheckBox,CheckBoxList,RadioButton,RadioButtonList。但是不清
/// 除如ListBox,DropDownList,因为这样的控件值对当前页面来说还可以用,一般这些控件里都是保存的字典数据。
/// Author:Kevin
/// www.jbxue.com
/// </summary>
/// <param name="page"> 指定的页面</param>
public static void ClearAllContent(System.Web.UI.Control page)
{
int nPageControls = page.Controls.Count;
for (int i = 0; i < nPageControls; i++)
{
foreach (System.Web.UI.Control control in page.Controls[i].Controls)
{
if (control.HasControls())
{
ClearAllText(control);
}
else
{
if (control is TextBox)
(control as TextBox).Text = "";

if (control is CheckBox)
(control as CheckBox).Checked = false;

if (control is RadioButtonList)
(control as RadioButtonList).SelectedIndex = -1;

if (control is RadioButton)
(control as RadioButton).Checked = false;

if (control is CheckBoxList)
{
foreach (ListItem item in (control as CheckBoxList).Items)
{
item.Selected = false;
}
}
}//if..else
}//foreach
}//for
}
#endregion

asp.net遍历页面中所有TextBox,并赋值为String.Empty的方法的更多相关文章

  1. 页面中如何引用外部的HTML(四种方法)

    页面中如何引用外部的HTML(四种方法) 一.总结 一句话总结:a.iframe标签        b.ajax引入代码片段        c.link import的方法导入        d.re ...

  2. 遍历页面上所有TextBox,并赋值为String.Empty

    //不含母板页 foreach (System.Web.UI.Control txtobj in this.Page.Controls)   {     if (txtobj.GetType().Na ...

  3. 修改页面中所有TextBox控件的样式--CSS

    1.HTML <div> TextBox<br /> <input type="text" name="name" value=& ...

  4. Asp.net在页面间传递大量数据(数据表)建议采用的方法

    能让数据在 两个不同站点之间传递吗? 我现在是想A站点的数据 传递到B站点....... 建议使用Cache (1)不太影响程序性能不太可能,你都说了,是大量数据.我举个例子,你是从A.aspx传到B ...

  5. asp.net(c#)开发中的文件上传组件uploadify的使用方法(带进度条)

    上文件传很常见,现在就文件上传利用HTML的File控件(uploadify)的,这里为大家介绍一下(uploadify)的一些使用方法.在目前Web开发中用的比较多的,可能uploadify(参考h ...

  6. H5页面中判断是安卓手机还是ios手机的方法;APP页面中嵌套的H5跳转到APP其他页面的方法。

    (一).在H5页面中,可以直接利用如下的方法来进行判断是安卓还是ios. var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linu ...

  7. C#遍历所有的Textbox控件并赋值为String.Empty

    foreach (Control control in this.Controls) { if (control.GetType().Name.Equals("TextBox")) ...

  8. ASP.NET aspx页面中 写C#脚本; ASP.NET 指令(<%@%>);

    1 <h2>Welcome</h2> <ul> <% for (int i = 0; i <= Convert.ToInt32(ViewData[&qu ...

  9. 在asp.net前台页面中引入命名空间 和连接数据库

    例如:<%@ Import Namespace="System.Data" %> 连接数据库 <% string strconn = "Data Sou ...

随机推荐

  1. C++服务器设计(一):基于I/O复用的Reactor模式

    I/O模型选择 在网络服务端编程中,一个常见的情景是服务器需要判断多个已连接套接字是否可读,如果某个套接字可读,则读取该套接字数据,并进行进一步处理. 在最常用的阻塞式I/O模型中,我们对每个连接套接 ...

  2. (原)Ubuntu16中编译caffe

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5797526.html 参考网址: http://caffe.berkeleyvision.org/in ...

  3. web中的中文字体的英文名称

    自从font-face出现以后,字体样式就不再是web开发者的难题了,但是对于移动端的中文来说,问题还是存在的,因为中文文件大小最少要3M+,即使选择性的加载某个字的字体,那也会出现不易替换的问题,所 ...

  4. vimdiff: 使用Vim中强大的文件diff功能[转]

    学习了一段时间的Vim,直到最近才发现Vim的diff功能是如此方便,对比代码变化再也不用到处去找diff软件或者依靠版本控制的diff了.强大的Vim. 下图是我在macVim中的diff效果. 下 ...

  5. Fragment与Activity相互传递数据:

    Activity向Fragment传递数据:在Activity中创建Bundle数据包,并调用Fragment的setArguments(Bundle bundle)方法即可将Bundle数据包传给F ...

  6. LLinux系统编程(10)——进程间通信之管道

    管道是Linux中很重要的一种通信方式,是把一个程序的输出直接连接到另一个程序的输入,常说的管道多是指无名管道,无名管道只能用于具有亲缘关系的进程之间,这是它与有名管道的最大区别.有名管道叫named ...

  7. ZOJ-3410Layton's Escape(优先队列+贪心)

    Layton's Escape Time Limit: 2 Seconds      Memory Limit: 65536 KB Professor Layton is a renowned arc ...

  8. ORACLE 中写入txt文本与从Txt文件中读入数据 修改表结构

    --创建一个表 DROP TABLE TEST CASCADE CONSTRAINTS ; CREATE TABLE TEST(A VARCHAR(30),B VARCHAR(30)); --查看具体 ...

  9. hdu 4649 Professor Tian 多校联合训练的题

    这题起初没读懂题意,悲剧啊,然后看了题解写完就AC了 题意是给一个N,然后给N+1个整数 接着给N个操作符(只有三种操作  即  或 ,与 ,和异或 |   &  ^ )这样依次把操作符插入整 ...

  10. iOS开发系列--打造自己的“美图秀秀”

    概述 在iOS中可以很容易的开发出绚丽的界面效果,一方面得益于成功系统的设计,另一方面得益于它强大的开发框架.今天我们将围绕iOS中两大图形.图像绘图框架进行介绍:Quartz 2D绘制2D图形和Co ...