ControlCollection.IndexOf(Control) 方法

定義

擷取集合中指定 Control 物件的索引。

public:
 virtual int IndexOf(System::Web::UI::Control ^ value);
public virtual int IndexOf (System.Web.UI.Control value);
abstract member IndexOf : System.Web.UI.Control -> int
override this.IndexOf : System.Web.UI.Control -> int
Public Overridable Function IndexOf (value As Control) As Integer

參數

value
Control

傳回索引的 Control

傳回

指定的伺服器控制項的索引。 如果伺服器控制項目前不是集合的成員,它會傳回 -1。

範例

下列程式碼範例會 IndexOf 使用 方法,在名為 myButton 的控制項集合中 ControlCollection 撰寫 控制項 myLiteralControlButton 索引位置。

// 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))

備註

使用這個方法來判斷 物件中指定之伺服器控制項的 ControlCollection 索引位置。

適用於

另請參閱