Control.FindControl Method (String)
Searches the current naming container for a server control with the specified id parameter.
[Visual Basic] Overloads Public Overridable Function FindControl( _ ByVal id As String _ ) As Control [C#] public virtual Control FindControl( string id ); [C++] public: virtual Control* FindControl( String* id ); [JScript] public function FindControl( id : String ) : Control;
Parameters
- id
- The identifier for the control to be found.
Return Value
The specified control, or a null reference (Nothing in Visual Basic) if the specified control does not exist.
Example
[Visual Basic, C#] 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.
[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"); } }
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
See Also
Control Class | Control Members | System.Web.UI Namespace | Control.FindControl Overload List