ControlCollection.AddAt(Int32, Control) 方法

定义

将指定的 Control 对象添加到集合中的指定索引位置。

public:
 virtual void AddAt(int index, System::Web::UI::Control ^ child);
public virtual void AddAt (int index, System.Web.UI.Control child);
abstract member AddAt : int * System.Web.UI.Control -> unit
override this.AddAt : int * System.Web.UI.Control -> unit
Public Overridable Sub AddAt (index As Integer, child As Control)

参数

index
Int32

数组中要添加子控件的位置。

child
Control

要添加到集合中的 Control

例外

child 参数不指定控件。

index 参数小于零或大于 Count 属性。

示例

下面的代码示例使用 AddAt 方法创建一个名为 的新 LiteralControlChildControl2 并将其添加到 myButton 控件的 ControlCollection 索引位置为 1 的集合。

// Create a LiteralControl and use the Add method to add it
// to a button's ControlCollection, then use the AddAt method
// to add another LiteralControl to the collection at the
// index location of 1.
LiteralControl myLiteralControl = new LiteralControl("ChildControl1");
myButton.Controls.Add(myLiteralControl);
myButton.Controls.AddAt(1,new LiteralControl("ChildControl2"));
Response.Write("<b>ChildControl2 is added at index 1</b>");

// Get the Index location of the myLiteralControl LiteralControl
// and write it to the page.
Response.Write("<br /><b>Index of the ChildControl myLiteralControl is " +
    "</b>" + myButton.Controls.IndexOf(myLiteralControl));
' Create a LiteralControl and use the Add method to add it
' to a button's ControlCollection, then use the AddAt method
' to add another LiteralControl to the collection at the
' index location of 1.
Dim myLiteralControl As LiteralControl =  _
    new LiteralControl("ChildControl1")
myButton.Controls.Add(myLiteralControl)
myButton.Controls.AddAt(1,new LiteralControl("ChildControl2"))
Response.Write("<b>ChildControl2 is added at index 1</b>")

' Get the Index location of the myLiteralControl LiteralControl
' and write it to the page.
Response.Write("<br /><b>Index of the ChildControl myLiteralControl is </b>" & _
                 myButton.Controls.IndexOf(myLiteralControl))

注解

添加的控件可以是任何 ASP.NET 服务器控件的实例、创建的自定义服务器控件或文本控件。

适用于

另请参阅