次の方法で共有


Control.FindControl メソッド

指定したサーバー コントロールの現在の名前付けコンテナを検索します。

オーバーロードの一覧

指定した id パラメータを使用して、サーバー コントロールの現在の名前付けコンテナを検索します。

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

このメンバは、.NET Framework インフラストラクチャのサポートを目的としています。独自に作成したコード内で直接使用することはできません。

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

使用例

[Visual Basic, C#] Button1_Click イベント ハンドラを定義する例を次に示します。このハンドラは FindControl メソッドを使用して、配置先のページの TextBox2ID プロパティを使ってコントロールを検索します。コントロールが見つかった場合は、 Parent プロパティでそのコントロールの親コントロールが確認され、その親コントロールの ID がページに書き込まれます。 TextBox2 が見つからなかった場合は、"Control Not Found" というテキストがページに書き込まれます。

[Visual Basic, C#] メモ   ここでは、FindControl のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 

   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] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

Control クラス | Control メンバ | System.Web.UI 名前空間