|
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Collections;
namespace DeKale.WebControls
{
/// <summary>
/// Builds up a tree of <see cref="System.Web.UI.Control"/> objects and
/// enables specific searches through the tree.
/// </summary>
class ControlTree : IEnumerable<Control>
{
private ControlCollection _collection;
private List<ControlTree> _childNodes = new List<ControlTree>();
/// <summary>Constructor</summary>
/// <param name="collection">The <see cref="System.Web.UI.ControlCollection"/> object</param>
public ControlTree(ControlCollection collection)
{
this._collection = collection;
foreach (Control control in collection)
{
if (control != null && control.Controls != null)
this._childNodes.Add(new ControlTree(control.Controls));
}
}
/// <summary>
/// Adds all the <see cref="Control"/> objects of itself and it's children to the
/// <paramref name="controlList"/>.
/// </summary>
/// <param name="controlList">A given <see cref="IList"/> in which all controls are added.</param>
protected void AppendList(IList<Control> controlList)
{
foreach (Control item in this._collection) controlList.Add(item);
foreach (ControlTree node in this._childNodes) node.AppendList(controlList);
}
#region IEnumerable<Control> Members
/// <summary>
/// Returns an <see cref="IEnumerator"/> that iterates through the <see cref="ControlTree"/>.
/// </summary>
/// <returns>The <see cref="IEnumerator"/></returns>
public IEnumerator<Control> GetEnumerator()
{
List<Control> list = new List<Control>();
this.AppendList(list);
foreach (Control item in list) yield return item;
}
#endregion
#region IEnumerable Members
/// <summary>
/// Returns an <see cref="IEnumerator"/> that iterates through the <see cref="ControlTree"/>.
/// </summary>
/// <returns>The <see cref="IEnumerator"/></returns>
IEnumerator IEnumerable.GetEnumerator()
{
List<Control> list = new List<Control>();
this.AppendList(list);
return list.GetEnumerator();
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.