我需要遍历所有的控件在我的asp.net网页,并做一些事情来控制。 在一种情况下我正在做一个巨大的字符串出来的页面,并通过电子邮件发送给自己,而在另一种情况下,我已把一切的cookie。
问题是masterpages和项目进行的内部控制它们的集合。 我希望能够在一个页面传递给方法,然后有一个方法是通用的,足以遍历最内层的内容页面的所有控件,并与他们合作。 我试着用递归这样做,但我的递归是不完整的。
我想一个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("<br />");
}
else if (c is ICheckBoxControl)
{
result.Append(controlID + ": " + ((ICheckBoxControl)c).Checked);
result.Append("<br />");
}
else if (c is ListControl)
{
result.Append(controlID + ": " + ((ListControl)c).SelectedValue);
result.Append("<br />");
}
else if (c.HasControls())
{
result.Append(controlToString(c));
}
//result.Append("<br />");
}
catch (Exception e)
{
}
}
return result.ToString();
}
如果没有的try / catch
你调用的对象是空的。
在线控件ID = .....
像page.Controls随你便只能通过控制第一级循环,但要记住一个控件可以是复合:如果您从文档的根元素开始你原来的方法是行不通的。 所以,你需要递归实现自己的目标。
public void FindTheControls(List<Control> 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);
}
}
}
}
我比较喜欢大卫Finleys LINQ方法的FindControl http://weblogs.asp.net/dfindley/archive/2007/06/29/linq-the-uber-findcontrol.aspx
public static class PageExtensions
{
public static IEnumerable<Control> 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<TextBox>()
.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所有控件。 即: <asp:TextBox ID="TextBoxEmail"...>
例如。 否则解析器不会检测到您的控制。 如果有人有分析更好的办法不知道/硬编码控件的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 + "<br />";
}
if (ctrl.ClientID.Contains("RadioButtonList"))
{
RadioButtonList rbl = (RadioButtonList)ctrl;
text += rbl.ID.Replace("RadioButtonList", "") + ": " + rbl.SelectedItem.Text + "<br />";
}
if (ctrl.ClientID.Contains("DropDownList"))
{
DropDownList ddl = (DropDownList)ctrl;
text += ddl.ID.Replace("DropDownList", "") + ": " + ddl.SelectedItem.Text + "<br />";
}
if (ctrl.ClientID.Contains("CheckBox"))
{
CheckBox cb1 = (CheckBox)ctrl;
text += cb1.ID.Replace("CheckBox", "") + ": " + cb1.Text + "<br />";
}
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<Control> lstControl = new List<Control>();
protected void Page_Load(object sender, EventArgs e)
{
}
private List<Label> getLabels() // Add all Lables to a list
{
List<Label> lLabels = new List<Label>();
foreach (Control oControl in Page.Controls)
{
GetAllControlsInWebPage(oControl);
}
foreach (Control oControl in lstControl)
{
if (oControl.GetType() == typeof(Label))
{
lLabels.Add((Label)oControl);
}
}
return lLabels;
}
protected void GetAllControlsInWebPage(Control oControl)
{
foreach (Control childControl in oControl.Controls)
{
lstControl.Add(childControl); //lstControl - Global variable
if (childControl.HasControls())
GetAllControlsInWebPage(childControl);
}
}
}