ControlCollection.Item Property
.NET Framework 2.0
Gets a reference to the server control at the specified index location in the ControlCollection object.
Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)
Assembly: System.Web (in system.web.dll)
| Exception type | Condition |
|---|---|
|
The index parameter is less than zero or greater than or equal to ControlCollection.Count. |
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.
<!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>
Community Additions
ADD
Show: