CatalogPartCollection.IndexOf(CatalogPart) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回集合特定成員的位置。
public:
int IndexOf(System::Web::UI::WebControls::WebParts::CatalogPart ^ catalogPart);
public int IndexOf (System.Web.UI.WebControls.WebParts.CatalogPart catalogPart);
member this.IndexOf : System.Web.UI.WebControls.WebParts.CatalogPart -> int
Public Function IndexOf (catalogPart As CatalogPart) As Integer
參數
- catalogPart
- CatalogPart
CatalogPart,為集合成員。
傳回
CatalogPart,CatalogPartCollection 的成員。
範例
下列程式代碼範例示範如何使用其 IndexOf 屬性來判斷集合成員CatalogPartCollection的位置。 如需執行範例所需的完整程式碼,請參閱類別概觀主題的 CatalogPartCollection 範例一節。
方法中的Button1_Click
程式代碼會建立名為 myParts
的新CatalogPartCollection物件。 方法會 IndexOf 使用 屬性來擷取控件的位置 PageCatalogPart ,然後變更 控件上的屬性值。
protected void Button1_Click(object sender, EventArgs e)
{
ArrayList list = new ArrayList(2);
list.Add(PageCatalogPart1);
list.Add(DeclarativeCatalogPart1);
// Pass an ICollection object to the constructor.
CatalogPartCollection myParts = new CatalogPartCollection(list);
foreach (CatalogPart catalog in myParts)
{
catalog.Description = "My " + catalog.DisplayTitle;
}
// Use the IndexOf property to locate a CatalogPart control.
int PageCatalogPartIndex = myParts.IndexOf(PageCatalogPart1);
myParts[PageCatalogPartIndex].ChromeType = PartChromeType.TitleOnly;
// Use the Contains method to see if a CatalogPart control exists.
if (myParts.Contains(PageCatalogPart1))
{
WebPart closedWebPart = null;
WebPartDescriptionCollection descriptions = PageCatalogPart1.GetAvailableWebPartDescriptions();
if (descriptions.Count > 0)
{
closedWebPart = PageCatalogPart1.GetWebPart(descriptions[0]);
closedWebPart.AllowClose = false;
}
}
// Use indexers to display the details of the CatalogPart controls.
Label1.Text = String.Empty;
Label1.Text =
"<h3>PageCatalogPart Details</h3>" +
"ID: " + myParts[0].ID + "<br />" +
"Count: " + myParts[0].GetAvailableWebPartDescriptions().Count;
Label1.Text +=
"<h3>DeclarativeCatalogPart Details</h3>" +
"ID: " + myParts["DeclarativeCatalogPart1"].ID + "<br />" +
"Count: " + myParts["DeclarativeCatalogPart1"].GetAvailableWebPartDescriptions().Count;
}
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim list As New ArrayList(2)
list.Add(PageCatalogPart1)
list.Add(DeclarativeCatalogPart1)
' Pass an ICollection object to the constructor.
Dim myParts As New CatalogPartCollection(list)
Dim catalog As CatalogPart
For Each catalog In myParts
catalog.Description = "My " + catalog.DisplayTitle
Next catalog
' Use the IndexOf property to locate a CatalogPart control.
Dim PageCatalogPartIndex As Integer = _
myParts.IndexOf(PageCatalogPart1)
myParts(PageCatalogPartIndex).ChromeType = PartChromeType.TitleOnly
' Use the Contains method to see if a CatalogPart control exists.
If myParts.Contains(PageCatalogPart1) Then
Dim closedWebPart As WebPart = Nothing
Dim descriptions As WebPartDescriptionCollection = _
PageCatalogPart1.GetAvailableWebPartDescriptions()
If descriptions.Count > 0 Then
closedWebPart = PageCatalogPart1.GetWebPart(descriptions(0))
closedWebPart.AllowClose = False
End If
End If
' Use indexers to display the details of the CatalogPart controls.
Label1.Text = String.Empty
Label1.Text = _
"<h3>PageCatalogPart Details</h3>" & _
"ID: " & myParts(0).ID + "<br />" & _
"Count: " & myParts(0).GetAvailableWebPartDescriptions().Count
Label1.Text += _
"<h3>DeclarativeCatalogPart Details</h3>" & _
"ID: " & myParts("DeclarativeCatalogPart1").ID & "<br />" & _
"Count: " & myParts("DeclarativeCatalogPart1") _
.GetAvailableWebPartDescriptions().Count
End Sub
在瀏覽器中載入頁面之後,您可以在 [顯示模式] 下拉式清單控件中選取 [目錄],將頁面切換為目錄模式。 按兩下 [ 顯示目錄元件屬性 ] 按鈕會存取物件, CatalogPartCollection 並顯示自主 CatalogPart 控制件的特定屬性。 按兩下 [頁面目錄 ] 連結以顯示控制元件的內容 PageCatalogPart 。 請注意,它只有標題且沒有框線,因為其 ChromeType 屬性值已變更為 TitleOnly 使用 IndexOf 屬性來擷取控件的程式代碼中。
備註
如果您在網頁元件頁面上有多個CatalogPart控件,而且您需要在集合中尋找特定控件,此方法IndexOf會很有用。