.NET Framework Class Library
ControlCollection.Item Property
Gets a reference to the server control at the specified index location in the ControlCollection object.
Assembly: System.Web (in System.Web.dll)
Syntax
Visual Basic
Public Overridable ReadOnly Property Item ( _ index As Integer _ ) As Control Get
C#
public virtual Control this[ int index ] { get; }
Visual C++
public: virtual property Control^ Item[int index] { Control^ get (int index); }
F#
abstract Item : Control override Item : Control
Parameters
- index
- Type: System.Int32
The location of the server control in the ControlCollection.
Exceptions
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException |
The index parameter is less than zero or greater than or equal to ControlCollection.Count. |
Examples
The following code example uses the Item property to specify the index location of a child control that is removed in a Remove method call. This is performed by the myButton.Controls.Remove syntax.
Visual Basic
<!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>
C#
<!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>
Version Information
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0Platforms
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
See Also