CatalogPartCollection 類別

定義

包含 CatalogPart 控制項集合,這個集合是用來提供使用者可以加入網頁中之 Web 伺服器控制項的類別目錄。 此類別無法獲得繼承。

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 用法。 此程式碼範例有四個部分:

  • 使用者控制項,可讓您變更網頁元件頁面上的顯示模式。

  • 名為 TextDisplayWebPart 的自訂 WebPart 控制項類別,該控制項會在網頁中參考,並包含在其中 CatalogPart 一個控制項中。

  • 參考控制項的 TextDisplayWebPart 網頁,包含控制項,其中包含區域中宣告之 Web 元件控制項集中的兩 CatalogPartCatalogZone 控制項,並包含事件驅動程式代碼來建立及操作 CatalogPartCollection 物件。

  • 說明當您在瀏覽器中載入程式碼範例時,程式碼範例的運作方式。

程式碼範例的第一個部分是使用者控制項。 使用者控制項的原始程式碼來自另一個主題。 若要讓此程式碼範例能夠運作,您必須從逐步解說 :變更網頁元件頁面上的顯示模式 主題中取得使用者控制項的 .ascx 檔案,並將檔案放在此程式碼範例中與 .aspx 頁面相同的資料夾中。

程式碼範例的第二個部分是 TextDisplayWebPart 控制項。 若要執行程式碼範例,您必須編譯此原始程式碼。 您可以明確地編譯它,並將產生的元件放在網站的 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

程式碼範例的第三個部分是網頁。 請注意, <asp:catalogzone> 頁面的 元素包含兩 CatalogPart 個控制項的宣告。 這些控制項會成為方法執行時 Button1_Click 所建立之自訂 CatalogPartCollection 物件的一部分。 控制項 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

當網頁元件頁面進入目錄模式時,區域會建立由控制群組成的 CatalogPartCatalogPartCollection 物件。 CatalogPart集合中的每個控制項都可以包含零個或多個 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)

從目標陣列的指定索引開始,將整個 ReadOnlyCollectionBase 複製到相容的一維 Array

(繼承來源 ReadOnlyCollectionBase)
ICollection.IsSynchronized

取得值,指出對 ReadOnlyCollectionBase 物件的存取是否為同步的 (執行緒安全)。

(繼承來源 ReadOnlyCollectionBase)
ICollection.SyncRoot

取得可用來同步存取 ReadOnlyCollectionBase 物件的物件。

(繼承來源 ReadOnlyCollectionBase)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。

AsParallel(IEnumerable)

啟用查詢的平行化作業。

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryable

適用於

另請參閱