Created
October 12, 2010 06:25
-
-
Save prabirshrestha/621755 to your computer and use it in GitHub Desktop.
Find Controls Recursive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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