Control.FindControl Control.FindControl Control.FindControl Control.FindControl Method

定義

在目前的命名容器搜尋指定的伺服器控制項。Searches the current naming container for the specified server control.

多載

FindControl(String) FindControl(String) FindControl(String) FindControl(String)

在目前命名容器搜尋具有指定 id 參數的伺服器控制項。Searches the current naming container for a server control with the specified id parameter.

FindControl(String, Int32) FindControl(String, Int32) FindControl(String, Int32) FindControl(String, Int32)

使用指定的 id 和有助於搜尋之 pathOffset 參數中所指定的整數,在目前的命名容器中搜尋伺服器控制項。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) FindControl(String) FindControl(String) 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 String String String

所要尋找的控制項識別項。The identifier for the control to be found.

傳回

指定的控制項,如果指定的控制項不存在,則為 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 Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。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. 如需如何在不知道控制項的立即容器時尋找控制項的相關資訊,請參閱如何:依識別碼存取伺服器控制項。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) FindControl(String, Int32) FindControl(String, Int32) FindControl(String, Int32)

使用指定的 id 和有助於搜尋之 pathOffset 參數中所指定的整數,在目前的命名容器中搜尋伺服器控制項。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 String String String

所要尋找的控制項識別項。The identifier for the control to be found.

pathOffset
Int32 Int32 Int32 Int32

在頁面控制項階層架構中,向上到達命名容器所需的控制項數目。The number of controls up the page control hierarchy needed to reach a naming container.

傳回

指定的控制項,如果指定的控制項不存在,則為 nullThe specified control, or null if the specified control does not exist.

適用於