Skip to content

Instantly share code, notes, and snippets.

@prabirshrestha
Created October 12, 2010 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prabirshrestha/621755 to your computer and use it in GitHub Desktop.
Save prabirshrestha/621755 to your computer and use it in GitHub Desktop.
Find Controls Recursive
namespace Helpers.Net.Web
{
using System.Web.UI;
public static partial class WebHelper
{
/// <summary>
/// Finds a Control Recursively.
/// </summary>
/// <typeparam name="T">Type control.</typeparam>
/// <param name="startingControl">Control to start finding from.</param>
/// <param name="id">ID of the control to search</param>
/// <returns>Returns the found control if found otherwise null.</returns>
public static T FindControlRecursive<T>(Control startingControl, string id)
where T : Control
{
T found = null;
foreach (Control activeControl in startingControl.Controls)
{
found = activeControl as T;
if (found == null)
found = FindControlRecursive<T>(activeControl, id);
else if (string.Compare(id, found.ID, true) != 0)
found = null;
if (found != null)
break;
}
return found;
}
/// <summary>
/// Finds a Control recursively.
/// </summary>
/// <param name="Root">
/// The Root.
/// </param>
/// <param name="Id">
/// The control id.
/// </param>
/// <returns>
/// Returns the found control if found otherwise null.
/// </returns>
public static Control FindControlRecursive(Control Root, string Id)
{
if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment