I need to iterate over all the controls on my asp.net webpage and do something for the control. In one case, I make a giant line from a page and send it by e-mail to myself, and in the other case I save all cookies.
The problem is the master pages and elements with collections of controls inside them. I want to be able to pass the page to a method, then this method will be general enough to iterate over all the controls on the innermost content page and work with them. I tried to do this with recursion, but my recursion is incomplete.
I want to pass the page object to a method and pass this method through all the controls on the innermost content page. How can I achieve this?
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)); }
Without try / catch
The reference to the object is not installed in the instance of the object.
On line controlID = .....
MAW74656
source share