Redigera

Dela via


CatalogPartCollection.Contains(CatalogPart) Method

Definition

Returns a value indicating whether a particular control exists in the collection.

public:
 bool Contains(System::Web::UI::WebControls::WebParts::CatalogPart ^ catalogPart);
public bool Contains (System.Web.UI.WebControls.WebParts.CatalogPart catalogPart);
member this.Contains : System.Web.UI.WebControls.WebParts.CatalogPart -> bool
Public Function Contains (catalogPart As CatalogPart) As Boolean

Parameters

catalogPart
CatalogPart

A CatalogPart that is checked to determine whether it is in the collection.

Returns

A Boolean value that indicates whether a particular control is in the collection.

Examples

The following code example demonstrates how you can determine whether a particular CatalogPart control is a member of a CatalogPartCollection object. 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 Contains method to determine whether the PageCatalogPart1 control exists, and if so, retrieves the control and changes a property value.

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 

When 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. Add the custom WebPart control to the page by selecting the check box next to it and clicking Add. Click Close to return the page to browse mode. On the control you just added, if you click the verbs menu (the down arrow that appears in the title bar) and then click Close, the control is removed from the page and added to the PageCatalogPart control. Return the page to catalog mode, and click the Page Catalog link to view the contents of the PageCatalogPart control. Notice that the control you closed has been added to it. Clicking the Display CatalogPart Properties button accesses the CatalogPartCollection object and displays certain properties of the contained CatalogPart controls. Select the check box next to the server control contained in the PageCatalogPart control, click Add to add it back to the page, and then click Close to return the page to browse mode. Click the verbs menu again on the control, and note that now the close verb is gone. It was removed by the code in the Button1_Click method, when it used the Contains method to check for PageCatalogPart1, and then set its AllowClose property to false.

Remarks

The Contains method enables you to determine whether a particular CatalogPart object is part of a CatalogPartCollection object.

Applies to

See also