Export (0) Print
Expand All

CatalogPartCollection.IndexOf Method

Returns the position of a particular member of the collection.

Namespace:  System.Web.UI.WebControls.WebParts
Assembly:  System.Web (in System.Web.dll)

public int IndexOf(
	CatalogPart catalogPart
)

Parameters

catalogPart
Type: System.Web.UI.WebControls.WebParts.CatalogPart

A CatalogPart that is a member of the collection.

Return Value

Type: System.Int32
A CatalogPart that is a member of the CatalogPartCollection.

The IndexOf method is useful if you have multiple CatalogPart controls on a Web Parts page, and you need to locate a particular control in the collection.

The following code example demonstrates how you can determine the position of a member of a CatalogPartCollection collection by using its IndexOf property. For the full code required to run the example, see the Example section of the CatalogPartCollection class overview topic.

The code in the Button1_Click method creates a new CatalogPartCollection object named myParts. The method uses the IndexOf property to retrieve the position of the PageCatalogPart control, and then changes a property value on the control.

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;
}

After you load the page in a browser, you can switch the page into catalog mode by selecting Catalog in the Display Mode drop-down list control. Clicking the Display CatalogPart Properties button accesses the CatalogPartCollection object and displays certain properties of the contained CatalogPart controls. Click the Page Catalog link to display the contents of the PageCatalogPart control. Note that it has only a title and no border, because its ChromeType property value was changed to TitleOnly in the code that used the IndexOf property to retrieve the control.

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft