Name

FindControl

Synopsis

Control = Page.FindControl(String)

Returns a reference to the control object whose name corresponds to a search string. The FindControl method is a member of the base Control class.

Parameters

Control

An instance of the Control class that represents the control that is found using the FindControl method. This control must be cast to the correct control type to access members that are specific to the control type.

String

A string containing the programmatic identifier of the control. This value is the same as the ID attribute of a declarative control, or in the case of controls created at runtime, is the same as the object name defined for the control.

Example

The example finds a control using its ID and changes its background color:

Sub Page_Load(  )
   Dim TheControl As Control = FindControl("Message")
   If Not TheControl Is Nothing Then
      Dim TheLabel As Label = CType(TheControl, Label)
      TheLabel.Text = "Found the label named Message!"
      TheLabel.BackColor = System.Drawing.Color.Blue
   End If
End Sub

Notes

The FindControl method, which is inherited from the Control class (from which the Page class is derived), is useful when dealing with nested controls or user controls that need to manipulate a control in their parent page. For example, code in a user control could call FindControl on the page containing the user control to locate and manipulate a control contained within the page (but outside the user control).

Get ASP.NET in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.