ControlCollection.Add(Control) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將指定的 Control 物件加入集合中。
public:
virtual void Add(System::Web::UI::Control ^ child);
public virtual void Add(System.Web.UI.Control child);
abstract member Add : System.Web.UI.Control -> unit
override this.Add : System.Web.UI.Control -> unit
Public Overridable Sub Add (child As Control)
參數
例外狀況
參數 child 並未指定控制點。
這是 ControlCollection 唯讀的。
範例
以下程式碼範例使用此 Add 方法,將一系列範本項目加入,項目數量取自伺服器控制項的視圖狀態,並加入自訂模板控制項。
// Override to create repeated items.
protected override void CreateChildControls() {
object o = ViewState["NumItems"];
if (o != null) {
// Clear any existing child controls.
Controls.Clear();
int numItems = (int)o;
for (int i=0; i < numItems; i++) {
// Create an item.
RepeaterItem item = new RepeaterItem(i, null);
// Initialize the item from the template.
ItemTemplate.InstantiateIn(item);
// Add the item to the ControlCollection.
Controls.Add(item);
}
}
}
' Override to create repeated items.
Protected Overrides Sub CreateChildControls()
Dim O As Object = ViewState("NumItems")
If Not (O Is Nothing)
' Clear any existing child controls.
Controls.Clear()
Dim I As Integer
Dim NumItems As Integer = CInt(O)
For I = 0 To NumItems - 1
' Create an item.
Dim Item As RepeaterItemVB = New RepeaterItemVB(I, Nothing)
' Initialize the item from the template.
ItemTemplate.InstantiateIn(Item)
' Add the item to the ControlCollection.
Controls.Add(Item)
Next
End If
End Sub
備註
新控制項會被加入序數索引陣列的末尾。 控制項可以是任何 ASP.NET 伺服器控制項的實例、你建立的自訂伺服器控制項,或是字面上的控制項。
若要在特定索引位置新增控制項,請使用該 AddAt 方法。