Export (0) Print
Expand All

Control.FindControl Method

Searches the current naming container for the specified server control.

Overload List

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

[Visual Basic] Overloads Public Overridable Function FindControl(String) As Control
[C#] public virtual Control FindControl(string);
[C++] public: virtual Control* FindControl(String*);
[JScript] public function FindControl(String) : Control;

This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

[Visual Basic] Overloads Protected Overridable Function FindControl(String, Integer) As Control
[C#] protected virtual Control FindControl(string, int);
[C++] protected: virtual Control* FindControl(String*, int);
[JScript] protected function FindControl(String, int) : Control;

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, C#] Note   This example shows how to use one of the overloaded versions of FindControl. For other examples that might be available, see the individual overload topics.
[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 Language Filter in the upper-left corner of the page.

See Also

Control Class | Control Members | System.Web.UI Namespace

Show:
© 2014 Microsoft