这是一篇很好的文章,但下面是一个修改版本,它通过给定属性收集控件
public List<Control> ListControlCollections(Page page, string propertyName)
{
List<Control> controlList = new List<Control>();
AddControls(page.Form.Controls, controlList, propertyName);
return controlList;
}
private void AddControls(ControlCollection controlCollection, List<Control> controlList, string propertyName)
{
foreach (Control c in controlCollection) {
PropertyInfo propertyToFind = c.GetType().GetProperty(propertyName);
if (propertyToFind != null) {
controlList.Add(c);
}
if (c.HasControls()) {
AddControls(c.Controls, controlList, propertyName);
}
}
}
要使用它:
List<Control> controlList = ListControlCollections("Text");
for (i=0; i < controlList.length; i++)
{
if (controlList[i].Text == string.empty)
{
// Do your logic here
}
else
{
// Do your logic here
}
}