ControlCollection.Item Property
Gets a reference to the server control at the specified index location in the ControlCollection object.
[C#] In C#, this property is the indexer for the ControlCollection class.
[Visual Basic] Public Overridable Default ReadOnly Property Item( _ ByVal index As Integer _ ) As Control [C#] public virtual Control this[ int index ] {get;} [C++] public: __property virtual Control* get_Item( int index ); [JScript] returnValue = ControlCollectionObject.Item(index); -or- returnValue = ControlCollectionObject(index);
[JScript] In JScript, you can use the default indexed properties defined by a type, but you cannot explicitly define your own. However, specifying the expando attribute on a class automatically provides a default indexed property whose type is Object and whose index type is String.
Arguments [JScript]
- index
- The location of the server control in the ControlCollection.
Parameters [Visual Basic, C#, C++]
- index
- The location of the server control in the ControlCollection.
Property Value
The reference to the control.
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentOutOfRangeException | The index parameter is less than zero or greater than or equal to the ControlCollection.Count property. |
Example
[Visual Basic, C#] The following 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] <html> <head> <script language="VB" runat="server"> Sub Page_Load( Sender as Object,e as EventArgs ) Response.Write("<h2><Center><b><font color=blue> Sample for ControlCollection Class</font></h2></Center></b>") 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))) Dim childControl as LiteralControl=CType(myEnumerator1.Current,LiteralControl) Response.Write("<b><br> This is the text of the child Control </b>:" _ + Server.HtmlEncode(childControl.Text)) End If End While myButton.Controls.Remove(myButton.Controls(0)) Response.Write("<br><b>ChildControl1 is removed</b>") Response.Write("<br><b> The count of ControlCollection </b>" _ +myButton.Controls.Count.ToString()) myButton.Controls.Clear() End Sub </script> </head> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:Button ID="myButton" Text="Sample ServerControl" Runat="server"></asp:Button> </form> </body> </html> [C#] <html> <head> <script language="cs" runat="server"> void Page_Load(Object Sender,EventArgs e) { Response.Write("<h2><Center><b><font color=blue> Sample for ControlCollection Class</font></h2></Center></b>"); 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("<b><br> This is the text of the child Control </b>:" + Server.HtmlEncode(childControl.Text)); } } myButton.Controls.Remove(myButton.Controls[0]); Response.Write("<br><b>ChildControl1 is removed</b>"); Response.Write("<br><b> The count of ControlCollection </b>" +myButton.Controls.Count); myButton.Controls.Clear(); } </script> </head> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:Button ID="myButton" Text="Sample ServerControl" Runat="server"></asp:Button> </form> </body> </html>
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family
See Also
ControlCollection Class | ControlCollection Members | System.Web.UI Namespace | Controls