CatalogPartCollection 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
包含 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 類別的多種使用方式。 此程式碼範例分為四個部分:
一個使用者控制功能,讓你能在網頁部分頁面上更改顯示模式。
一個名為 WebPart的自訂
TextDisplayWebPart控制類別,該類別在網頁中被引用,並包含在其中一個CatalogPart控制項中。一個參考
TextDisplayWebPart控制項的網頁,包含CatalogZone一個控制項,並在區域中宣告了 Web Parts 控制集中的兩個CatalogPart控制項,並且包含事件驅動的程式碼,用以建立和操作物件。CatalogPartCollection說明範例程式碼在瀏覽器載入時是如何運作的。
程式碼範例的第一部分是使用者控制。 使用者控制的原始碼來自另一個主題。 為了讓這個程式碼範例運作,你需要從「 攻略:更改網頁部分頁面的顯示模式 」主題中取得使用者控制的 .ascx 檔案,並將該檔案放入與本範例中.aspx頁相同的資料夾中。
程式碼範例的第二部分是控制。TextDisplayWebPart 要執行程式碼範例,你必須編譯這個原始碼。 你可以明確編譯,並將結果組合放到你網站的 Bin 資料夾或全域組合快取中。 或者,你也可以把原始碼放在網站的App_Code資料夾,執行時會動態編譯。 若想了解如何展示兩種編譯方法,請參見《 攻略:開發與使用自訂網頁伺服器控制法》。 請注意,這個控制項有一個名為 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 控制項的宣告。 這些控制項會成為方法執行時CatalogPartCollection建立的自訂Button1_Click物件的一部分。 控制 PageCatalogPart 項包含先前由使用者在執行時關閉的網頁伺服器控制項。 控制項中的 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 。 注意你關閉的控制現在出現在那裡。 點擊「 顯示目錄部分屬性 」按鈕可存取物件 CatalogPartCollection 並顯示所包含 CatalogPart 控制項的特定屬性。
備註
該 CatalogPartCollection 類別是一個唯讀的控制項集合 CatalogPart ,通常由 CatalogZoneBase 區域用來追蹤該區域所包含的 CatalogPart 控制集合。
當網頁部分頁面進入目錄模式時,區域會建立 CatalogPartCollection 一個包含 CatalogPart 控制項的新物件。 集合中的每個 CatalogPart 控制項可以包含零個或多個網頁伺服器控制項的參考,這些控制項以可用伺服器控制項目錄的形式顯示。
例如,如果你需要對一組CatalogPartCollection控制項進行大規模操作,你可以為自己的程式化使用建立CatalogPart一組控制項。 即使物件 CatalogPartCollection 是唯讀,你仍可對集合中參考的底層控制項做程式化變更。
建構函式
| 名稱 | Description |
|---|---|
| CatalogPartCollection() |
初始化一個新的、空的類別實例 CatalogPartCollection 。 |
| CatalogPartCollection(CatalogPartCollection, ICollection) |
透過傳遞CatalogPartCollection區域中現有ICollection控制項的集合及額外的控制項集合,初始化類別的新CatalogPart實例。 |
| CatalogPartCollection(ICollection) |
透過傳遞CatalogPartCollection一組ICollection控制項來初始化該類別的新CatalogPart實例。 |
欄位
| 名稱 | Description |
|---|---|
| Empty |
參考一個靜態、唯讀、空的集合實例。 |
屬性
| 名稱 | Description |
|---|---|
| Count |
取得該實例中包含 ReadOnlyCollectionBase 的元素數量。 (繼承來源 ReadOnlyCollectionBase) |
| InnerList |
取得實例中包含的 ReadOnlyCollectionBase 元素清單。 (繼承來源 ReadOnlyCollectionBase) |
| Item[Int32] |
根據集合中的位置,獲得或設定集合中的成員。 |
| Item[String] |
根據唯一的字串識別碼回傳集合成員。 |
方法
| 名稱 | Description |
|---|---|
| Contains(CatalogPart) |
回傳一個值,表示集合中是否存在特定控制項。 |
| CopyTo(CatalogPart[], Int32) |
將集合複製到一組 CatalogPart 物件陣列。 |
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetEnumerator() |
回傳一個枚舉器,會遍歷該 ReadOnlyCollectionBase 實例。 (繼承來源 ReadOnlyCollectionBase) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| IndexOf(CatalogPart) |
回傳集合中特定成員的位置。 |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |
明確介面實作
| 名稱 | Description |
|---|---|
| ICollection.CopyTo(Array, Int32) |
從目標陣列的指定索引開始,將整個 ReadOnlyCollectionBase 複製到相容的一維 Array。 (繼承來源 ReadOnlyCollectionBase) |
| ICollection.IsSynchronized |
取得值,指出對 ReadOnlyCollectionBase 物件的存取是否同步處理 (線程安全)。 (繼承來源 ReadOnlyCollectionBase) |
| ICollection.SyncRoot |
取得對象,這個物件可用來同步存取 ReadOnlyCollectionBase 物件。 (繼承來源 ReadOnlyCollectionBase) |
擴充方法
| 名稱 | Description |
|---|---|
| AsParallel(IEnumerable) |
啟用查詢的平行處理。 |
| AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |
| Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
| OfType<TResult>(IEnumerable) |
根據指定的型別篩選 IEnumerable 的專案。 |