循环遍历asp.net网页上的所有控件

我需要遍历我的asp.net网页中的所有控件并对控件执行某些操作。 在一个例子中,我正在从页面中创建一个巨大的字符串并通过电子邮件发送给自己,在另一个例子中,我将所有内容保存到cookie中。

问题是masterpages和包含控件集合的项目。 我希望能够将一个Page传递给该方法,然后让该方法具有足够的通用性,以遍历最内层内容页面中的所有控件并使用它们。 我尝试用递归来做这个,但我的递归是不完整的。

我想将一个Page对象传递给一个方法,并让该方法循环遍历最里面的内容页面中的所有控件。 我怎样才能做到这一点?

private static String controlToString(Control control) { StringBuilder result = new StringBuilder(); String controlID = String.Empty; Type type = null; foreach (Control c in control.Controls) { try { controlID = c.ID.ToString(); if (c is IEditableTextControl) { result.Append(controlID + ": " + ((IEditableTextControl)c).Text); result.Append("
"); } else if (c is ICheckBoxControl) { result.Append(controlID + ": " + ((ICheckBoxControl)c).Checked); result.Append("
"); } else if (c is ListControl) { result.Append(controlID + ": " + ((ListControl)c).SelectedValue); result.Append("
"); } else if (c.HasControls()) { result.Append(controlToString(c)); } //result.Append("
"); } catch (Exception e) { } } return result.ToString(); }

没有Try / catch

你调用的对象是空的。

在线控制ID = …..

如果从文档的根元素开始,原始方法将不起作用:类似于page.Controls,因为您只会遍历第一级控件,但请记住控件可以是复合的。 所以你需要递归来解决这个问题。

  public void FindTheControls(List foundSofar, Control parent) { foreach(var c in parent.Controls) { if(c is IControl) //Or whatever that is you checking for { foundSofar.Add(c); if(c.Controls.Count > 0) { this.FindTheControls(foundSofar, c); } } } } 

我更喜欢David Finleys对FindControl的linq方法http://weblogs.asp.net/dfindley/archive/2007/06/29/linq-the-uber-findcontrol.aspx

 public static class PageExtensions { public static IEnumerable All(this ControlCollection controls) { foreach (Control control in controls) { foreach (Control grandChild in control.Controls.All()) yield return grandChild; yield return control; } } } 

用法:

 // get the first empty textbox TextBox firstEmpty = accountDetails.Controls .All() .OfType() .Where(tb => tb.Text.Trim().Length == 0) .FirstOrDefault(); // and focus it if (firstEmpty != null) firstEmpty.Focus(); 
 foreach (Control ctlMaster in Page.Controls) { if (ctlMaster is MasterPage) { foreach (Control ctlForm in ctlMaster.Controls) { if (ctlForm is HtmlForm) { foreach (Control ctlContent in ctlForm.Controls) { if (ctlContent is ContentPlaceHolder) { foreach (Control ctlChild in ctlContent.Controls) { //Do something! } } } } } } } 

这应该做到这一点。 虽然您可能需要进行一些检查以确保您实际处理的是正确的ContentPlaceHolder(如果有多个)。

 sub getcontrols(byref c as control, byref allControls as list(of control) if c isnot nothing allcontrols.add(c) if c.controls.count>0 then for each ctrl as control in c.controls getcontrols(ctrl,allcontrols) next end if 

这对我有用..

只需确保您使用下面显示的前缀开始标识所有控件。 即: 例如。 否则解析器将无法检测到您的控件。 如果有人在不知道/硬编码控件的ID的情况下有更好的解析方法,那就更甜蜜了。

 protected String GetControls(Control control) { //Get text from form elements String text = ""; foreach (Control ctrl in control.Controls) { if (ctrl.ClientID.Contains("TextBox")) { TextBox tb = (TextBox)ctrl; text += tb.ID.Replace("TextBox", "") + ": " + tb.Text + "
"; } if (ctrl.ClientID.Contains("RadioButtonList")) { RadioButtonList rbl = (RadioButtonList)ctrl; text += rbl.ID.Replace("RadioButtonList", "") + ": " + rbl.SelectedItem.Text + "
"; } if (ctrl.ClientID.Contains("DropDownList")) { DropDownList ddl = (DropDownList)ctrl; text += ddl.ID.Replace("DropDownList", "") + ": " + ddl.SelectedItem.Text + "
"; } if (ctrl.ClientID.Contains("CheckBox")) { CheckBox cb1 = (CheckBox)ctrl; text += cb1.ID.Replace("CheckBox", "") + ": " + cb1.Text + "
"; } if (ctrl.HasControls()) text += GetControls(ctrl); } return text; }

并调用它,传递Page对象……

 String log; foreach (Control ctrl in Page.Controls) log += GetControls(ctrl); 

请找到以下代码。 这应该可以帮助您完成所需的所有控制。 您应该能够使用网页控件以及ASP.NET控件。

 public partial class Default : System.Web.UI.Page { List lstControl = new List(); protected void Page_Load(object sender, EventArgs e) { } private List