Locating ASP.NET Controls

How to locate an ASP.NET control loaded somewhere else on a page.

Each control belongs to the control tree below the Page control. If you define server-side IDs for these controls, you can easily access a control on the page and, if needed, use values of its properties in another control.

For this purpose you can create a method to recursively iterate controls in the control tree and search for the control with the ID passed in parameter of this method. The method returns 'null' if no match is found.

Use the samples below to see how it can be implemented:

  1. From the "Functions" perspective, create two User Control functions, for example, "Test.A" and "Test.B".
  2. Copy the code from the samples below to the functions correspondingly.
  3. Edit a page.
  4. Insert the "Test.A" and "Test.B" User Control functions on the page ("Insert" / "Functions").
  5. If needed, switch to "Source" mode and wrap the inserted functions with <asp:form/>.
  6. Save and publish the page.

(Alternatively, in Step 2 you can replace the existing files in ~/App_Data/UserControls/Test/ ith the sample files below - provided you keep the above suggested names for the functions.)

A.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="A.ascx.cs" Inherits="C1Function" %>

<asp:TextBox ID="TextBoxA" runat="server"></asp:TextBox>
<asp:Button ID="ButtonA" runat="server" Text="Copy to B" onclick="ButtonA_Click" />
Download A.ascx

A.ascx.cs:

using System;
using Composite.Functions;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class C1Function : Composite.AspNet.UserControlFunction
{
    public override string FunctionDescription
    {
        get 
        { 
            return "A demo function that searches for a control with an ID specified."; 
        }
    }

    protected void  ButtonA_Click(object sender, EventArgs e)
    {
        // Grabbing a label from another control
        Label bLabel = (Label)FindControlRecursive(this .Page, "BLabel");

        bLabel.Text = 
			(bLabel != null ) ? "Control A's text box says "  + TextBoxA.Text : "Control B not found";        
    }    
    
    private  Control FindControlRecursive(Control root, string id)
    {
        if (root.ID == id)
            return root;

        foreach  (Control c in root.Controls)
        {
            Control t = FindControlRecursive(c, id);
            if  (t != null)
                return t;
        }

        return null;
    }
}
Download A.ascx.cs

B.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="B.ascx.cs" Inherits="C1Function" %>

<asp:Label ID="BLabel"  runat="server"></asp:Label>
Download B.ascx

B.ascx.cs:

using System;
using Composite.Functions;

public partial class C1Function : Composite.AspNet.UserControlFunction
{
    public override string FunctionDescription
    {
        get 
        { 
            return "A demo function that outputs a message passed into it."; 
        }
    }

}
Download B.ascx.cs

In the above sample, by clicking the "Copy to B" button the Control A gets the value from the textbox, locates Control B on the page and sets its label's text to the value.