次の方法で共有


Control.Parent プロパティ

ページ コントロールの階層構造における、サーバー コントロールの親コントロールへの参照を取得します。

Public Overridable ReadOnly Property Parent As Control
[C#]
public virtual Control Parent {get;}
[C++]
public: __property virtual Control* get_Parent();
[JScript]
public function get Parent() : Control;

プロパティ値

サーバー コントロールの親コントロールへの参照。

解説

ページが要求されると常に、そのページ上のサーバー コントロールの階層構造が構築されます。このプロパティを使用すると、その階層構造における現在のサーバー コントロールの親コントロールを決定し、それに対してプログラムを作成できます。

使用例

[Visual Basic, C#] ページ内の新しい Control オブジェクト myControl1FindControl メソッドで指定したコントロールに設定する例を次に示します。コントロールが返された場合、コードは Parent プロパティを使用して myControl1 が格納されているコントロールを識別します。親コントロールが存在する場合は、"The parent of the text box is" という文字列が親コントロールの ID プロパティと連結され、 Page に書き込まれます。親コントロールが見つからない場合は、"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


[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# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 2000, Windows XP Professional, Windows Server 2003 ファミリ

参照

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