For when you need to access a control from within a user control where the ID of said control is passed in as a parameter. That was a mouthful. Basically, if you create a custom user control that takes in the ID of another control (let's say a TextBox) the user control cannot find a handle to that control using the standard Page.FindControl("TextBox1"). If TextBox1 exists on the main page (whether or not it's on a master page) and your custom control needs to access TextBox1, it cannot be accessed thru the Page.FindControl method directly. Luckily, there is recursion.
Here is the method to find the control:
public Control FindControlByID(ControlCollection controls, string id)
{
Control found = null;
foreach(Control control in controls)
{
if(control.HasControls())
{
found = FindControlByID(control.Controls, id);
if(found != null)
{
break;
}
}
if(control.ID == id)
{
found = control;
break;
}
}
return found;
}
An example of this would be if you had a user control that had to populate a textbox that was passed in as a property.
Widget.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Widget.ascx.cs" Inherits="_controls_Widget" %>
Widget.ascx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _controls_Widget : System.Web.UI.UserControl
{
private string _controlToUpdate;
public string ControlToUpdate
{
get { return _controlToUpdate; }
set { _controlToUpdate = value; }
}
public override void DataBind()
{
if(_controlToUpdate != "")
{
Control control = FindControlByID(Page.Controls, _controlToUpdate);
if(control is TextBox)
{
TextBox txt = (TextBox)control;
txt.Text = "UPDATE HERE";
}
}
base.DataBind();
}
public Control FindControlByID(ControlCollection controls, string id)
{
Control found = null;
foreach(Control control in controls)
{
if(control.HasControls())
{
found = FindControlByID(control.Controls, id);
if(found != null)
{
break;
}
}
if(control.ID == id)
{
found = control;
break;
}
}
return found;
}
}
Test.aspx
<%@ Page Language="C#" MasterPageFile="~/_masterPages/main.master" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" Title="Untitled Page" %>
<%@ Register TagPrefix="uc" TagName="Widget" Src="~/_controls/Widget.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainBody" Runat="Server">
<asp:TextBox ID="TextBox1" runat="server" />
<uc:Widget ID="Widget1" runat="server" ControlToUpdate="TextBox1" />
</asp:Content>
Test.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Widget1.DataBind();
}
}
Enjoy!