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. 如需如何尋找控制項,當您不知道其立即的容器資訊,請參閱How to:存取伺服器控制項的 IDFor 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.

適用於