CatalogPartCollection クラス

定義

エンド ユーザーが Web ページに追加できる Web サーバー コントロールのカタログを提供するために使用される CatalogPart コントロールのコレクションを格納します。 このクラスは継承できません。

public ref class CatalogPartCollection sealed : System::Collections::ReadOnlyCollectionBase
public sealed class CatalogPartCollection : System.Collections.ReadOnlyCollectionBase
type CatalogPartCollection = class
    inherit ReadOnlyCollectionBase
Public NotInheritable Class CatalogPartCollection
Inherits ReadOnlyCollectionBase
継承
CatalogPartCollection

次のコード例は、 CatalogPartCollection クラスのいくつかの使用方法を示しています。 このコード例には、次の 4 つの部分があります。

  • Web パーツ ページの表示モードを変更できるユーザー コントロール。

  • Web ページで参照され、WebPart コントロールのいずれかに含まれる、TextDisplayWebPartという名前のカスタム CatalogPart コントロールのクラス。

  • TextDisplayWebPart コントロールを参照する Web ページには、ゾーンで宣言された Web パーツ コントロール セットの 2 つのCatalogZone コントロールを持つCatalogPart コントロールが含まれており、CatalogPartCollection オブジェクトを作成および操作するためのイベント ドリブン コードが含まれています。

  • ブラウザーに読み込むときにコード例がどのように動作するかを説明します。

コード例の最初の部分は、ユーザー コントロールです。 ユーザー コントロールのソース コードは、別のトピックから取得されます。 このコード例を機能させるには、「 チュートリアル: Web パーツ ページでの表示モードの変更 」トピックからユーザー コントロールの .ascx ファイルを取得し、このコード例の .aspx ページと同じフォルダーにファイルを配置する必要があります。

コード例の 2 番目の部分は、 TextDisplayWebPart コントロールです。 コード例を実行するには、このソース コードをコンパイルする必要があります。 明示的にコンパイルし、結果のアセンブリを Web サイトの Bin フォルダーまたはグローバル アセンブリ キャッシュに配置できます。 または、ソース コードをサイトの App_Code フォルダーに配置して、実行時に動的にコンパイルすることもできます。 両方のコンパイル方法を示すチュートリアルについては、「 チュートリアル: カスタム Web サーバー コントロールの開発と使用」を参照してください。 コントロールには ContentText という名前のプロパティがあることに注意してください。このプロパティは、ユーザーがテキスト ボックスに入力した値を保持します。

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class TextDisplayWebPart : WebPart
  {
    private String _contentText = null;
    TextBox input;
    Label DisplayContent;
    Literal lineBreak;

    [Personalizable(), WebBrowsable]
    public String ContentText
    {
      get { return _contentText; }
      set { _contentText = value; }
    }

    protected override void CreateChildControls()
    {
      Controls.Clear();
      DisplayContent = new Label();
      DisplayContent.BackColor = Color.LightBlue;
      DisplayContent.Text = this.ContentText;
      this.Controls.Add(DisplayContent);

      lineBreak = new Literal();
      lineBreak.Text = @"<br />";
      Controls.Add(lineBreak);

      input = new TextBox();
      this.Controls.Add(input);
      Button update = new Button();
      update.Text = "Set Label Content";
      update.Click += new EventHandler(this.submit_Click);
      this.Controls.Add(update);
    }

    private void submit_Click(object sender, EventArgs e)
    {
      // Update the label string.
      if (!string.IsNullOrEmpty(input.Text))
      {
        _contentText = input.Text + @"<br />";
        input.Text = String.Empty;
        DisplayContent.Text = this.ContentText;
      }
    }
  }
}
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

Namespace Samples.AspNet.VB.Controls

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class TextDisplayWebPart
    Inherits WebPart
    Private _contentText As String = Nothing
    Private _fontStyle As String = Nothing
    Private input As TextBox
    Private DisplayContent As Label
    Private lineBreak As Literal

    <Personalizable(), WebBrowsable()> _
    Public Property ContentText() As String
      Get
        Return _contentText
      End Get
      Set(ByVal value As String)
        _contentText = value
      End Set
    End Property

    Protected Overrides Sub CreateChildControls()
      Controls.Clear()
      DisplayContent = New Label()
      DisplayContent.BackColor = Color.LightBlue
      DisplayContent.Text = Me.ContentText
      Me.Controls.Add(DisplayContent)

      lineBreak = New Literal()
      lineBreak.Text = "<br />"
      Controls.Add(lineBreak)

      input = New TextBox()
      Me.Controls.Add(input)
      Dim update As New Button()
      update.Text = "Set Label Content"
      AddHandler update.Click, AddressOf Me.submit_Click
      Me.Controls.Add(update)

    End Sub

    Private Sub submit_Click(ByVal sender As Object, _
                             ByVal e As EventArgs)
      ' Update the label string.
      If input.Text <> String.Empty Then
        _contentText = input.Text + "<br />"
        input.Text = String.Empty
        DisplayContent.Text = Me.ContentText
      End If

    End Sub

  End Class

End Namespace

コード例の 3 番目の部分は Web ページです。 ページの <asp:catalogzone> 要素に、2 つの CatalogPart コントロールの宣言が含まれていることに注意してください。 これらのコントロールは、CatalogPartCollection メソッドの実行時に作成されるカスタム Button1_Click オブジェクトの一部になります。 PageCatalogPart コントロールには、実行時にユーザーが以前に閉じた Web サーバー コントロールが含まれています。 PageCatalogPart コントロール内のコントロールは、ページに戻すことができます。 DeclarativeCatalogPart コントロールには、カスタム TextDisplayWebPart コントロールの宣言が含まれています。 ページがカタログ モードの場合、ユーザーはページに TextDisplayWebPart コントロールを追加して、通常のブラウズ モードで使用できるようにします。

<%@ Page Language="C#" %>
<%@ register TagPrefix="uc1" 
  TagName="DisplayModeMenuCS" 
  Src="DisplayModeMenuCS.ascx" %>
<%@ Register TagPrefix="aspSample"
  Namespace="Samples.AspNet.CS.Controls" 
  Assembly="TextDisplayWebPartCS" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  // <snippet2>
  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;
  }
  // </snippet2>
</script> 
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>PageCatalogPart Details</title>
</head>
<body>
  <form id="form1" runat="server">
    <asp:WebPartManager ID="WebPartManager1" runat="server" />
    <uc1:DisplayModeMenuCS ID="DisplayModeMenu1" runat="server" />
    <asp:WebPartZone ID="WebPartZone1" runat="server">
      <ZoneTemplate>
        <asp:BulletedList 
          ID="BulletedList1" 
          Runat="server"
          DisplayMode="HyperLink" 
          Title="Favorite Links" >
          <asp:ListItem Value="http://msdn.microsoft.com">
            MSDN
          </asp:ListItem>
          <asp:ListItem Value="http://www.asp.net">
            ASP.NET
          </asp:ListItem>
          <asp:ListItem Value="http://www.msn.com">
            MSN
          </asp:ListItem>
        </asp:BulletedList>
      </ZoneTemplate>
    </asp:WebPartZone>
    <asp:CatalogZone ID="CatalogZone1" runat="server">
      <ZoneTemplate>
        <asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" runat="server">
          <WebPartsTemplate>
            <aspSample:TextDisplayWebPart runat="server" 
              id="TextDisplayWebPart1"
              Title="Text Display WebPart" />
          </WebPartsTemplate>
        </asp:DeclarativeCatalogPart>
        <asp:PageCatalogPart ID="PageCatalogPart1" runat="server" />  
      </ZoneTemplate>
    </asp:CatalogZone>
    <hr />
    <asp:Button ID="Button1" 
      runat="server" 
      Text="Display CatalogPart Properties" 
      OnClick="Button1_Click"/>
    <br />
    <asp:Label ID="Label1" runat="server" Text="" /> 
  </form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ register TagPrefix="uc1" 
  TagName="DisplayModeMenuVB" 
  Src="DisplayModeMenuVB.ascx" %>
<%@ Register TagPrefix="aspSample"
  Namespace="Samples.AspNet.VB.Controls" 
  Assembly="TextDisplayWebPartVB" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
' <snippet2>
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 
' </snippet2>
</script>  
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>PageCatalogPart Details</title>
</head>
<body>
  <form id="form1" runat="server">
    <asp:WebPartManager ID="WebPartManager1" runat="server" />
    <uc1:DisplayModeMenuVB ID="DisplayModeMenu1" runat="server" />
    <asp:WebPartZone ID="WebPartZone1" runat="server">
      <ZoneTemplate>
        <asp:BulletedList 
          ID="BulletedList1" 
          Runat="server"
          DisplayMode="HyperLink" 
          Title="Favorite Links" >
          <asp:ListItem Value="http://msdn.microsoft.com">
            MSDN
          </asp:ListItem>
          <asp:ListItem Value="http://www.asp.net">
            ASP.NET
          </asp:ListItem>
          <asp:ListItem Value="http://www.msn.com">
            MSN
          </asp:ListItem>
        </asp:BulletedList>
      </ZoneTemplate>
    </asp:WebPartZone>
    <asp:CatalogZone ID="CatalogZone1" runat="server">
      <ZoneTemplate>
        <asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" runat="server">
          <WebPartsTemplate>
            <aspSample:TextDisplayWebPart runat="server" 
              id="TextDisplayWebPart1"
              Title="Text Display WebPart" />
          </WebPartsTemplate>
        </asp:DeclarativeCatalogPart>
        <asp:PageCatalogPart ID="PageCatalogPart1" runat="server" />  
      </ZoneTemplate>
    </asp:CatalogZone>
    <hr />
    <asp:Button ID="Button1" 
      runat="server" 
      Text="Display CatalogPart Properties" 
      OnClick="Button1_Click"/>
    <br />
    <asp:Label ID="Label1" runat="server" Text="" /> 
  </form>
</body>
</html>

ブラウザーでページを読み込むときに、[表示モード] ドロップダウン リスト コントロールで [カタログ] を選択して、ページを カタログモード に切り替えることができます。 カスタム WebPart コントロールをページに追加するには、その横にあるチェック ボックスをオンにして [ 追加] をクリックします。 [ 閉じる ] をクリックしてページを参照モードに戻します。 追加したコントロールで、動詞メニュー (タイトル バーに表示される下矢印) をクリックし、[ 閉じる] をクリックすると、コントロールがページから削除され、 PageCatalogPart コントロールに追加されます。 ページをカタログ モードに戻し、[ ページ カタログ ] リンクをクリックして、 PageCatalogPart コントロールの内容を表示します。 閉じたコントロールがそこに表示されます。 [ CatalogPart プロパティの表示 ] ボタンをクリックすると、 CatalogPartCollection オブジェクトにアクセスし、含まれている CatalogPart コントロールの特定のプロパティが表示されます。

注釈

CatalogPartCollection クラスは、CatalogPart コントロールの読み取り専用コレクションです。通常は、ゾーンに含まれるCatalogZoneBase コントロールのセットを追跡するためにCatalogPart ゾーンによって使用されます。

Web パーツ ページがカタログ モードになると、ゾーンによって、CatalogPartCollection コントロールで構成される新しいCatalogPart オブジェクトが作成されます。 コレクション内の各 CatalogPart コントロールには、使用可能なサーバー コントロールのカタログ形式で表示される、0 個以上の Web サーバー コントロールへの参照を含めることができます。

たとえば、一連のCatalogPartCollection コントロールに対して一括操作を実行する必要がある場合は、独自のプログラムで使用するコントロールのCatalogPart コレクションを作成できます。 CatalogPartCollection オブジェクトは読み取り専用ですが、コレクションで参照されている基になるコントロールにプログラムで変更を加えることができます。

コンストラクター

名前 説明
CatalogPartCollection()

CatalogPartCollection クラスの新しい空のインスタンスを初期化します。

CatalogPartCollection(CatalogPartCollection, ICollection)

ゾーン内の既存のCatalogPartCollection コントロールのICollection コレクションとコントロールの追加コレクションを渡して、CatalogPart クラスの新しいインスタンスを初期化します。

CatalogPartCollection(ICollection)

CatalogPartCollection コントロールのICollection コレクションを渡して、CatalogPart クラスの新しいインスタンスを初期化します。

フィールド

名前 説明
Empty

コレクションの静的な読み取り専用の空のインスタンスを参照します。

プロパティ

名前 説明
Count

ReadOnlyCollectionBase インスタンスに含まれる要素の数を取得します。

(継承元 ReadOnlyCollectionBase)
InnerList

ReadOnlyCollectionBase インスタンスに含まれる要素の一覧を取得します。

(継承元 ReadOnlyCollectionBase)
Item[Int32]

コレクション内の位置に基づいて、コレクションのメンバーを取得または設定します。

Item[String]

一意の文字列識別子に基づいてコレクションのメンバーを返します。

メソッド

名前 説明
Contains(CatalogPart)

コレクション内に特定のコントロールが存在するかどうかを示す値を返します。

CopyTo(CatalogPart[], Int32)

コレクションを CatalogPart オブジェクトの配列にコピーします。

Equals(Object)

指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetEnumerator()

ReadOnlyCollectionBase インスタンスを反復処理する列挙子を返します。

(継承元 ReadOnlyCollectionBase)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IndexOf(CatalogPart)

コレクションの特定のメンバーの位置を返します。

MemberwiseClone()

現在の Objectの簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

名前 説明
ICollection.CopyTo(Array, Int32)

ターゲット配列の指定したインデックスから始まる互換性のある 1 次元ReadOnlyCollectionBaseに、Array全体をコピーします。

(継承元 ReadOnlyCollectionBase)
ICollection.IsSynchronized

ReadOnlyCollectionBase オブジェクトへのアクセスが同期されているかどうかを示す値を取得します (スレッド セーフ)。

(継承元 ReadOnlyCollectionBase)
ICollection.SyncRoot

ReadOnlyCollectionBase オブジェクトへのアクセスを同期するために使用できるオブジェクトを取得します。

(継承元 ReadOnlyCollectionBase)

拡張メソッド

名前 説明
AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryableに変換します。

Cast<TResult>(IEnumerable)

IEnumerable の要素を指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定した型に基づいて、IEnumerable の要素をフィルター処理します。

適用対象

こちらもご覧ください