다음을 통해 공유


ControlCollection.Item[Int32] 속성

정의

ControlCollection 개체의 지정된 인덱스 위치에서 서버 컨트롤에 대한 참조를 가져옵니다.

public:
 virtual property System::Web::UI::Control ^ default[int] { System::Web::UI::Control ^ get(int index); };
public virtual System.Web.UI.Control this[int index] { get; }
member this.Item(int) : System.Web.UI.Control
Default Public Overridable ReadOnly Property Item(index As Integer) As Control

매개 변수

index
Int32

ControlCollection의 서버 컨트롤 위치입니다.

속성 값

컨트롤에 대한 참조입니다.

예외

index 매개 변수가 0보다 작거나 Count보다 크거나 같은 경우

예제

다음 코드 예제에서는 합니다 Item[] 인덱스 위치의 자식 컨트롤을 지정 하려면 속성에서 제거 되었습니다를 Remove 메서드를 호출 합니다. 이 작업에서 수행 됩니다는 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">
   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">
    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>

적용 대상

추가 정보