次の方法で共有


ControlCollection.Item プロパティ

ControlCollection オブジェクトの指定されたインデックス位置にあるサーバー コントロールへの参照を取得します。

名前空間: System.Web.UI
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Public Overridable ReadOnly Default Property Item ( _
    index As Integer _
) As Control
'使用
Dim instance As ControlCollection
Dim index As Integer
Dim value As Control

value = instance(index)
public virtual Control this [
    int index
] { get; }
public:
virtual property Control^ default [int] {
    Control^ get (int index);
}
/** @property */
public Control get_Item (int index)
適用できません。

パラメータ

プロパティ値

コントロールへの参照。

例外

例外の種類 条件

ArgumentOutOfRangeException

index パラメータが 0 未満か、ControlCollection.Count 以上です。

使用例

Remove メソッド呼び出しで削除される子コントロールのインデックス位置を、Item プロパティを使用して指定するコード例を次に示します。これは、myButton.Controls.Remove 構文で実行されています。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs)

        Response.Write("<h2>Sample for ControlCollection Class</h2>")
        Dim myLiteralControl As LiteralControl _
            = New LiteralControl("ChildControl1")

        myButton.Controls.Add(myLiteralControl)
        myButton.Controls.AddAt(1, New LiteralControl("ChildControl2"))

        Dim myControlCollectionArray As System.Array = _
            Array.CreateInstance(GetType(Object), _
                myButton.Controls.Count)
        myButton.Controls.CopyTo(myControlCollectionArray, 0)

        Dim myEnumerator1 As IEnumerator = _
            myControlCollectionArray.GetEnumerator()

        While myEnumerator1.MoveNext()
            Dim myObject As Object = myEnumerator1.Current

            If myObject.GetType().Equals(GetType(LiteralControl)) Then
                Dim childControl As LiteralControl _
                    = CType(myEnumerator1.Current, LiteralControl)
                Response.Write("<p style=""font-weight:bold"">")
                Response.Write("This is the  text of the child Control:" _
                    & Server.HtmlEncode(childControl.Text))
            End If
        End While

        myButton.Controls.Remove(myButton.Controls(0))
        Response.Write("</p><p style=""font-weight:bold"">ChildControl1 is removed")
        Response.Write("<br />The count of ControlCollection = " _
            & myButton.Controls.Count.ToString() & "</p>")
        myButton.Controls.Clear()

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Sample for ControlCollection Class</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Button ID="myButton" Text="Sample ServerControl" 
            Runat="server"></asp:Button>
    
    </div>
    </form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
   void Page_Load(Object Sender,EventArgs e)
   {
       Response.Write("<h2>Sample for ControlCollection Class</h2>");

       LiteralControl myLiteralControl 
           = new LiteralControl("ChildControl1");

       myButton.Controls.Add(myLiteralControl);
       myButton.Controls.AddAt(1,new LiteralControl("ChildControl2"));

       System.Array myControlCollectionArray 
           = Array.CreateInstance(typeof(object), 
               myButton.Controls.Count);
       myButton.Controls.CopyTo(myControlCollectionArray,0);

       IEnumerator myEnumerator1 = 
           myControlCollectionArray.GetEnumerator();

       while (myEnumerator1.MoveNext())
       {
           object myObject = myEnumerator1.Current;

           if(myObject.GetType().Equals(typeof(LiteralControl)))
           {
               LiteralControl childControl = 
                   (LiteralControl)myEnumerator1.Current;
               Response.Write("<p style=\"font-weight:bold\">");
               Response.Write("This is the  text of the child Control:" 
                   + Server.HtmlEncode(childControl.Text));
           }
       }

       myButton.Controls.Remove(myButton.Controls[0]);
       Response.Write("</p><p style=\"font-weight:bold\">");
       Response.Write("ChildControl1 is removed<br />");
       Response.Write("The count of ControlCollection = "
           + myButton.Controls.Count.ToString() + "</p>");
       myButton.Controls.Clear();
   }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:Button ID="myButton" Text="Sample ServerControl" 
            Runat="server"></asp:Button>

    </div>
    </form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    void Page_Load(Object Sender, EventArgs e)
    {
        get_Response().Write(
            "<h2>Sample for ControlCollection Class</h2>");
        
        LiteralControl myLiteralControl =  
            new LiteralControl("ChildControl1");
        myButton.get_Controls().Add(myLiteralControl);
        myButton.get_Controls().AddAt(1, 
            new LiteralControl("ChildControl2"));

        System.Array myControlCollectionArray = 
            Array.CreateInstance(
                Object.class.ToType(), 
                myButton.get_Controls().get_Count());
        myButton.get_Controls().CopyTo(myControlCollectionArray, 0);
        
        IEnumerator myEnumerator1 = 
            myControlCollectionArray.GetEnumerator();
        
        while(myEnumerator1.MoveNext()) 
        {
            Object myObject = myEnumerator1.get_Current();
            if (myObject.GetType().Equals(
                LiteralControl.class.ToType())) 
            {
                LiteralControl childControl = 
                    ((LiteralControl)(myEnumerator1.
                    get_Current()));
                get_Response().Write(
                    "<p style=\"font-weight:bold\"><br />");
                get_Response().Write("The count of ControlCollection = ");
                get_Response().Write(get_Server().HtmlEncode(
                    childControl.get_Text()) + "</p>");
            }
        }
        myButton.get_Controls().Remove(
            myButton.get_Controls().get_Item(0));
        get_Response().Write(
            "<br /><b>ChildControl1 is removed</b>");
        get_Response().Write(
            "<br /><b> The count of ControlCollection </b>" 
            + myButton.get_Controls().get_Count());
        myButton.get_Controls().Clear();
    } //Page_Load
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Sample for ControlCollection Class</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Button ID="myButton" Text="Sample ServerControl" 
            Runat="server"></asp:Button>

    </div>
    </form>
</body>
</html>

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

ControlCollection クラス
ControlCollection メンバ
System.Web.UI 名前空間
Control.Controls プロパティ