Control.FindControl Method (String)
Assembly: System.Web (in system.web.dll)
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.
| Topic | Location |
|---|---|
| How to: Reference ASP.NET Master Page Content | Building ASP .NET Web Applications |
| How to: Reference ASP.NET Master Page Content | Building ASP .NET Web Applications |
| How to: Reference ASP.NET Master Page Content |
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 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 Studio). |
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"); } }
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.get_Parent();
this.get_Response().Write("Parent of the text box is : "
+ myControl2.get_ID());
}
else {
this.get_Response().Write("Control not found");
}
}//Button1_Click
Security Note: