Use the API to find controls on a form.
using System;
using System.Collections.Generic;
using System.Web.UI;
using PNMsoft.Sequence.Forms.Web.UI;
namespace CustomCodeBehind
{
/// <summary>
/// Extending the Control element
/// </summary>
public static class ControlExtender
{
public static T FindControl<T>(this Control control, string id) where T :Control
{
T result = default(T);
if (string.Equals(control.ID, id, StringComparison.OrdinalIgnoreCase))
{
if (control is T)
{
return (T)control;
}
return result;
}
try
{
foreach (Control c in control.Controls)
{
T ctr = c.FindControl<T>(id);
if (ctr != default(T))
{
return ctr;
}
}
}
catch (Exception)
{
}
return result;
}
/// <summary>
/// Find all control of type T recursively
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="control"></param>
/// <param name="controls"></param>
internal static void FindControl<T>(this Control control, List<T> controls)where T : Control
{
if (controls == null) controls = new List<T>();
if (control is T)
{
controls.Add((T)control);
}
foreach (Control c in control.Controls)
{
FindControl<T>(c, controls);
}
}
/// <summary>
/// Add a recursively search for contained control by its name
/// </summary>
/// <param name="currentCtrl">The container</param>
/// <param name="controlId">The control id</param>
/// <returns>The searched control</returns>
public static Control FindControlByIdRecursive(this Control currentCtrl,string controlId)
{
if (currentCtrl == null)
return null;
Control ctrl = currentCtrl.FindControl(controlId);
if (ctrl == null)
foreach (Control subCtrl in currentCtrl.Controls)
{
ctrl = subCtrl.FindControlByIdRecursive(controlId);
if (ctrl != null)
break;
}
return ctrl;
}
/// <summary>
/// Get a list of controls from the page based on their type
/// </summary>
/// <param name="currentCtrl">The parent control to look in it</param>
/// <param name="controlType">The type of the controls to be retrieved.</param>
/// <returns>A list of controls selected by their type</returns>
public static List<Control> FindControlsByTypeRecursive(this ControlcurrentCtrl, string controlType)
{
List<Control> ctrls = new List<Control>();
Type type = Type.GetType(controlType);
if (currentCtrl.GetType() == type)
ctrls.Add(currentCtrl);
else
foreach (Control subCtrl in currentCtrl.Controls)
{
ctrls.AddRange(subCtrl.FindControlsByTypeRecursive(controlType));
}
return ctrls;
}
/// <summary>
/// Returns the parent Form control of a control. In case there is no parent form, returns null
/// </summary>
/// <param name="currentCtrl">The current control to start from</param>
/// <returns></returns>
public static FormControl GetParentFormCtrl(this Control currentCtrl)
{
if (currentCtrl == null)
return null;
var ctrl = currentCtrl.Parent;
if (ctrl is FormControl)
return (ctrl as FormControl);
return ctrl.GetParentFormCtrl();
}
public static T GetParentForm<T>(this Control ctrl)
where T : Control
{
if (!(ctrl is T))
return GetParentForm<T>(ctrl.Parent);
return (T)ctrl;
}
/// <summary>
/// Find a parent control (container) by its id. Returns null when no parent is found
/// </summary>
/// <param name="currentCtrl">The current control to start fro</param>
/// <param name="controlId">The parent id to look for</param>
/// <returns></returns>
public static Control GetParentControlById(this Control currentCtrl, stringcontrolId)
{
if (currentCtrl == null)
return null;
var ctrl = currentCtrl.Parent;
return ctrl.ID == controlId ? ctrl : ctrl.GetParentControlById(controlId);
}
}
}