本文介绍下,如何用.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. 写个点击input框 下方弹出月份时间等

    <input type="text" name="test" id="test" value="" "& ...

  2. iOS-设计模式之代理反向传值

    代理设计模式就是自己的方法自己不实现,让代理对象去实现. 可以让多个类实现一组方法. 委托模式的好处在于: 1.避免子类化带来的过多的子类以及子类与父类的耦合 2.通过委托传递消息机制实现分层解耦 代 ...

  3. -canOpenURL: failed for URL

    这在 Xcode 6.4 + iOS 8 时,是不会有的情况,原因是[为了强制增强数据访问安全, iOS9 默认会把所有从NSURLConnection . CFURL . NSURLSession发 ...

  4. python中json的操作示例

    先上一段示例 # -*- coding: cp936 -*- import json #构造一个示例数据,并打印成易读样式 j = {} j["userName"]="a ...

  5. jQuery对checkbox的各种操作

    //注意: 操作checkbox的checked,disabled属性时jquery1.6以前版本用attr,1.6以上(包含)建议用prop //1.根据id获取checkbox $("# ...

  6. HTML5简单入门系列(七)

    前言 本篇详细介绍canvas画布的API.说是详细介绍,也只是一些常用的API及简单实例和说明.LZ本人也还没有看完全部,下篇会介绍剩余的一些内容. 本篇的示例中,LZ加入了注释,为的是只简单介绍A ...

  7. 负载均衡集群之LVS的DR模型详解(Diretor Routing)

    LVS的默认模型:默认模型DR DR模型原理图--> 在讲DR模型要点之前,需要了解网络的相关知识: 接收的报文拆解顺序:帧(MAC)-->数据包(IP)-->数据报文(port) ...

  8. 负载均衡集群之LVS配置命令

    ipvs/ipvsadm 添加集群服务--> ipvsadm -A|E -t|u|f VIP[:Port] -s scheduler [-p timeout] [-O] [-M netmask] ...

  9. Octorber 21st

    Octorber 21st Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  10. iOS开发之网络篇-各种网络状态码

    1xx消息 这一类型的状态码,代表请求已被接受,需要继续处理.这类响应是临时响应,只包含状态行和某些可选的响应头信息,并以空行结束.由于HTTP/1.0协议中没有定义任何1xx状态码,所以除非在某些试 ...