This documentation is archived and is not being maintained.

Page.FindControl Method (String)

Searches the page naming container for a server control with the specified identifier.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public override Control FindControl(
	string id


Type: System.String

The identifier for the control to be found.

Return Value

Type: System.Web.UI.Control
The specified control, or null if the specified control does not exist.

The FindControl method can be used to access a control whose ID is not available at design time. The method searches only the page's immediate, or top-level, container; it does not recursively search for controls in naming containers contained on the page. To access controls in a subordinate naming container, call the FindControl method of that container.

The following code example demonstrates how to use the FindControl method to locate controls inside templates. In this example, two Repeater controls are defined; each shows a different way to catch the Click event of a LinkButton inside the repeater item template.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">

  private class RepeaterObject
    private string _string;

    public RepeaterObject(string label)
      _string = label;

    public string RepeaterLabel
      get { return _string; }
      set { _string = value; }

  protected void Page_Load()
    if (!IsPostBack)
      ArrayList al = new ArrayList();

      al.Add(new RepeaterObject("foo1"));
      al.Add(new RepeaterObject("foo2"));
      al.Add(new RepeaterObject("foo3"));
      Repeater1.DataSource = al;
      Repeater2.DataSource = al;

  // This occurs for Repeater1 and originates from LinkButton onClick. 
  protected void OnMyCommand1(object sender, CommandEventArgs e)
    LinkButton b = sender as LinkButton;
    if (b != null)
      Label c = (Label)b.Parent.FindControl("foo");
      if (c != null)
        c.Text = "text changed in handler";
        c.ForeColor = System.Drawing.Color.Green;

  // This occurs for Repeater2 and comes from the Repeater onItemCommand. 
  protected void OnMyCommand2(object sender, RepeaterCommandEventArgs e)
    Label l = (Label)e.Item.FindControl("foo");
    if (l != null)
      l.Text = "text changed in handler";
      l.ForeColor = System.Drawing.Color.Red;

<html xmlns="" >
<head runat="server">
    <title>Page FindControl Example</title>
    <form id="form1" runat="server">

    <asp:Panel ID="Panel1" runat="server" >
        This repeater sample shows the bubbled event and FindControl when the repeater item OnCommand event occurs.<br />
        <asp:Repeater ID="Repeater1" runat="server">
                <asp:Label runat="server" ID="foo" Text='<%#Eval("RepeaterLabel")%>' />&nbsp;
                <asp:LinkButton Text="Change" runat="server" OnCommand="OnMyCommand1" /> <br />
        <hr />

        This repeater shows the bubbled event and FindControl when the repeater OnItemCommand event occurs. <br />
        <asp:Repeater ID="Repeater2" runat="server" OnItemCommand="OnMyCommand2">
                <asp:Label runat="server" ID="foo" Text='<%#Eval("RepeaterLabel")%>' />&nbsp;
                <asp:LinkButton Text="Change" runat="server" /> <br />


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0