遍历和查找容器内的控件

前台界面:



后台代码:

  1 using  System;
  2 using  System.Collections;
  3 using  System.ComponentModel;
  4 using  System.Data;
  5 using  System.Drawing;
  6 using  System.Web;
  7 using  System.Web.SessionState;
  8 using  System.Web.UI;
  9 using  System.Web.UI.WebControls;
 10 using  System.Web.UI.HtmlControls;
 11
 12 namespace  AspNetTest.Common
 13 {
 14    /// <summary>
 15    /// MoreControls 的摘要说明。
 16    /// </summary>

 17    public class Controls : System.Web.UI.Page
 18    {
 19        protected System.Web.UI.WebControls.Panel Panel1;
 20        protected System.Web.UI.WebControls.Panel Panel2;
 21        protected System.Web.UI.WebControls.TextBox TextBox2;
 22        protected System.Web.UI.WebControls.Label Label1;
 23        protected System.Web.UI.WebControls.Button Button1;
 24        protected System.Web.UI.WebControls.Label Label2;
 25        protected System.Web.UI.WebControls.TextBox TextBox3;
 26        protected System.Web.UI.WebControls.Button Button2;
 27        protected System.Web.UI.WebControls.TextBox TextBox1;
 28    
 29        private bool bFound = false;
 30        private void Page_Load(object sender, System.EventArgs e)
 31        {
 32            // 在此处放置用户代码以初始化页面
 33        }

 34
 35        Web 窗体设计器生成的代码
 57
 58        private void Button1_Click(object sender, System.EventArgs e)
 59        {
 60            GetTextBoxValues(Panel1);
 61        }

 62        private void GetTextBoxValues(Control c)
 63        {
 64            if(c is TextBox)
 65            {
 66                Label1.Text += c.ID + " 的值:" + ((TextBox)c).Text + "<br>";
 67            }

 68            else
 69            {
 70                for(int i=0; i<c.Controls.Count; i++)
 71                {
 72                    GetTextBoxValues(c.Controls[i]);
 73                }

 74            }

 75        }

 76        private void SearchTextBoxValue(Control c, string SearchId)
 77        {
 78            if(c is TextBox)
 79            {
 80                if(c.ID == SearchId)
 81                {
 82                    Label1.Text = c.ID + " 的值:" + ((TextBox)c).Text + "<br>";
 83                    bFound = true;
 84                }

 85            }

 86            else
 87            {
 88                for(int i=0; i<c.Controls.Count; i++)
 89                {
 90                    SearchTextBoxValue(c.Controls[i], SearchId);
 91                }

 92            }

 93        }

 94
 95        private void Button2_Click(object sender, System.EventArgs e)
 96        {
 97            string SearchTextBoxId = TextBox3.Text.Trim();
 98            SearchTextBoxValue(Panel1, SearchTextBoxId);
 99            if(bFound == false)
100            {
101                Label1.Text = "<font color=red>没有找到ID值为 " + SearchTextBoxId + "的文本框</font>";
102            }

103        }

104    }

105}

106


效果图1:



效果图2:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值