CatalogPartCollection.IndexOf(CatalogPart) 方法

定义

返回集合中特定成员的位置。

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 

在浏览器中加载页面后,可以通过在“显示模式”下拉列表控件中选择“目录”,将页面切换到目录模式。 单击“ 显示 CatalogPart 属性” 按钮将 CatalogPartCollection 访问对象并显示包含 CatalogPart 控件的某些属性。 单击 “页面目录” 链接以显示控件的内容 PageCatalogPart 。 请注意,它只有标题,没有边框,因为它ChromeType的属性值在使用 属性检索控件的代码IndexOf中更改为 TitleOnly

注解

IndexOf如果 Web 部件页上有多个CatalogPart控件,并且需要在集合中查找特定控件,则 方法非常有用。

适用于

另请参阅