---
title: "遍历页面上的所有控件"
author: "cjc"
date: "2007-11-20T09:43:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-11-20T09:43:00Z"
id: 402732
url: "https://cjc.iteye.com/blog/402732"
---

&lt;script&gt;function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}&lt;/script&gt;

/// &lt;summary&gt;&lt;br&gt; /// 采用递归的方法来遍历页面控件&lt;br&gt; /// &lt;/summary&gt;  
/// &lt;param name="parent"&gt;  
protected void ErgodicChildrenControls(Control parent)  
{

foreach (Control c in parent.Controls)  
{

//此处写有关控件的代码，例如

//if (c is HtmlInputButton)  
// {

// HtmlInputButton btn = (HtmlInputButton)c;

// if (c.id =="save")

// {  
// c.Visble=true;

// }  
// }

if (c.Controls.Count &gt; 0) // 判断该控件是否有下属控件。  
{  
ErgodicChildrenControls(c); //递归，访问该控件的下属控件集。  
}  
}  
}

调用的方法：

protected void Page\_Load(object sender, EventArgs e)  
{

ErgodicChildrenControls(this);  
}

注意：该方法只能遍历服务器（即 runat=server）控件