Control.FindControl 方法

定义

在当前的命名容器中搜索指定的服务器控件。Searches the current naming container for the specified server control.

重载

FindControl(String)

在当前的命名容器中搜索带指定 id 参数的服务器控件。Searches the current naming container for a server control with the specified id parameter.

FindControl(String, Int32)

使用指定的 idpathOffset 参数(该参数有助于搜索)中指定的整数在当前命名容器中搜索服务器控件。Searches the current naming container for a server control with the specified id and an integer, specified in the pathOffset parameter, which aids in the search. 不应重写此版本的 FindControl 方法。You should not override this version of the FindControl method.

FindControl(String)

在当前的命名容器中搜索带指定 id 参数的服务器控件。Searches the current naming container for a server control with the specified id parameter.

public:
 virtual System::Web::UI::Control ^ FindControl(System::String ^ id);
public virtual System.Web.UI.Control FindControl (string id);
abstract member FindControl : string -> System.Web.UI.Control
override this.FindControl : string -> System.Web.UI.Control
Public Overridable Function FindControl (id As String) As Control

参数

id
String

要查找的控件的标识符。The identifier for the control to be found.

返回

Control

指定的控件,如果指定的控件不存在则为 nullThe specified control, or null if the specified control does not exist.

示例

下面的示例定义了一个 Button1_Click 事件处理程序。The following example defines a Button1_Click event handler. 在调用时,此处理程序使用 FindControl 方法在 ID TextBox2 包含页上查找属性为的控件。When invoked, this handler uses the FindControl method to locate a control with an ID property of TextBox2 on the containing page. 如果找到了该控件,则使用属性确定其父控件, Parent 并将父控件的 ID 写入页面。If the control is found, its parent is determined using the Parent property and the parent control's ID is written to the page. 如果 TextBox2 找不到,则将 "找不到控制" 写入页面。If TextBox2 is not found, "Control Not Found" is written to the page.

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。This example has a text box that accepts user input, which is a potential security threat. 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 有关详细信息,请参阅脚本侵入概述For more information, see Script Exploits Overview.

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 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

注解

使用 FindControl 从代码隐藏页中的函数访问控件,以访问位于另一个容器内的控件,或者用于访问调用方无法直接访问目标控件的其他情况下的控件。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. 仅当指定容器直接包含控件时,此方法才会找到控件;也就是说,该方法不在控件内的控件层次结构中进行搜索。This method will find a control only if the control is directly contained by the specified container; that is, the method does not search throughout a hierarchy of controls within controls. 有关如何在不知道控件的即时容器的情况下查找控件的信息,请参阅如何:按 ID 访问服务器控件For information about how to find a control when you do not know its immediate container, see How to: Access Server Controls by ID.

另请参阅

适用于

产品 已引入

FindControl(String, Int32)

使用指定的 idpathOffset 参数(该参数有助于搜索)中指定的整数在当前命名容器中搜索服务器控件。Searches the current naming container for a server control with the specified id and an integer, specified in the pathOffset parameter, which aids in the search. 不应重写此版本的 FindControl 方法。You should not override this version of the FindControl method.

protected:
 virtual System::Web::UI::Control ^ FindControl(System::String ^ id, int pathOffset);
protected virtual System.Web.UI.Control FindControl (string id, int pathOffset);
abstract member FindControl : string * int -> System.Web.UI.Control
override this.FindControl : string * int -> System.Web.UI.Control
Protected Overridable Function FindControl (id As String, pathOffset As Integer) As Control

参数

id
String

要查找的控件的标识符。The identifier for the control to be found.

pathOffset
Int32

页面控件层次结构上到达命名容器所需的控件的数量。The number of controls up the page control hierarchy needed to reach a naming container.

返回

Control

指定的控件,如果指定的控件不存在则为 nullThe specified control, or null if the specified control does not exist.

适用于

产品 已引入