Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Control Class
Control Methods
 FindControl Method (String)
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Control..::.FindControl Method (String)

Searches the current naming container for a server control with the specified id parameter.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Visual Basic (Declaration)
Public Overridable Function FindControl ( _
    id As String _
) As Control
Visual Basic (Usage)
Dim instance As Control
Dim id As String
Dim returnValue As Control

returnValue = instance.FindControl(id)
C#
public virtual Control FindControl(
    string id
)
Visual C++
public:
virtual Control^ FindControl(
    String^ id
)
JScript
public function FindControl(
    id : String
) : Control

Parameters

id
Type: System..::.String
The identifier for the control to be found.

Return Value

Type: System.Web.UI..::.Control
The specified control, or nullNothingnullptra null reference (Nothing in Visual Basic) if the specified control does not exist.

Use FindControl to access a control from a function in a code-behind page, to access a control that is inside another container, or in other circumstances where the target control is not directly accessible to the caller.

TopicLocation
How to: Reference ASP.NET Master Page ContentBuilding ASP .NET Web Applications
How to: Reference ASP.NET Master Page ContentBuilding ASP .NET Web Applications
How to: Reference ASP.NET Master Page ContentBuilding ASP .NET Web Applications in Visual Studio
How to: Reference ASP.NET Master Page ContentBuilding ASP .NET Web Applications in Visual Studio

The following example defines a Button1_Click event handler. When invoked, this handler uses the FindControl method to locate a control with an ID property of TextBox2 on the containing page. If the control is found, its parent is determined using the Parent property and the parent control's ID is written to the page. If TextBox2 is not found, "Control Not Found" is written to the page.

Security noteSecurity Note:

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

Visual Basic
Private Sub Button1_Click(sender As Object, MyEventArgs As EventArgs)
' Find control on page.
Dim myControl1 As Control = FindControl("TextBox2")
If (Not myControl1 Is Nothing)
   ' Get control's parent.
   Dim myControl2 As Control = myControl1.Parent
   Response.Write("Parent of the text box is : " & myControl2.ID)
Else
   Response.Write("Control not found.....")
End If
End Sub

C#
private void Button1_Click(object sender, EventArgs MyEventArgs)
{
      // Find control on page.
      Control myControl1 = FindControl("TextBox2");
      if(myControl1!=null)
      {
         // Get control's parent.
         Control myControl2 = myControl1.Parent;
         Response.Write("Parent of the text box is : " + myControl2.ID);
      }
      else
      {
         Response.Write("Control not found");
      }
}

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, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker