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

這是 ControlCollection 唯讀的。

範例

以下程式碼範例使用該 AddAt 方法建立新 LiteralControl 命名 ChildControl2 ,並將其加入 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 伺服器控制項的實例、你建立的自訂伺服器控制項,或是字面上的控制項。

適用於

另請參閱