Share via


CatalogPartCollection.IndexOf(CatalogPart) Méthode

Définition

Retourne la position d'un membre particulier de la collection.

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

Paramètres

catalogPart
CatalogPart

CatalogPart qui est membre de la collection.

Retours

CatalogPart qui est membre de CatalogPartCollection.

Exemples

L’exemple de code suivant montre comment déterminer la position d’un membre d’une CatalogPartCollection collection à l’aide de sa IndexOf propriété . Pour obtenir le code complet requis pour exécuter l’exemple, consultez la section Exemple de la rubrique Vue d’ensemble de la CatalogPartCollection classe.

Le code dans la Button1_Click méthode crée un CatalogPartCollection objet nommé myParts. La méthode utilise la IndexOf propriété pour récupérer la position du PageCatalogPart contrôle, puis modifie une valeur de propriété sur le contrôle.

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 

Après avoir chargé la page dans un navigateur, vous pouvez basculer la page en mode catalogue en sélectionnant Catalogue dans le contrôle de liste déroulante Mode d’affichage . Cliquez sur le bouton Afficher les propriétés du cataloguePart pour accéder à l’objet CatalogPartCollection et affiche certaines propriétés des contrôles contenus CatalogPart . Cliquez sur le lien Catalogue de pages pour afficher le contenu du PageCatalogPart contrôle. Notez qu’il n’a qu’un titre et aucune bordure, car sa ChromeType valeur de propriété a été modifiée TitleOnly en dans le code qui a utilisé la IndexOf propriété pour récupérer le contrôle.

Remarques

La IndexOf méthode est utile si vous avez plusieurs CatalogPart contrôles sur une page de composants WebPart et que vous devez localiser un contrôle particulier dans la collection.

S’applique à

Voir aussi