CatalogPart クラス
アセンブリ: System.Web (system.web.dll 内)
構文
解説
CatalogPart クラスは、CatalogZoneBase ゾーンに配置される特殊なコントロールの基本クラスです。これらの派生 CatalogPart コントロールには、エンド ユーザーが Web ページに追加できる Web サーバー コントロールのカタログが用意されています。エンド ユーザーがサーバー コントロールの追加や移動によって Web ページの機能を柔軟に変更できるようにする場合は、Web アプリケーションで CatalogPart コントロールを使用します。
カタログは、ユーザーが Web ページに追加できる 1 つ以上の Web サーバー コントロール (WebPart コントロール、ASP.NET サーバー コントロール、カスタム コントロール、ユーザー コントロールなど) のリストです。カタログには、エンド ユーザー向けの説明テキスト、各サーバー コントロールの説明テキスト、サーバー コントロールを選択してページに追加するためのヘルパー コントロール、共通ヘッダー、共通フッター、共通の境界線、多数のスタイル属性など、多数の共通スタイル特性が格納されています。
次の表に示されるように、Web パーツ コントロール セットで提供される 3 種類の CatalogPart コントロールが存在します。それぞれの種類の CatalogPart コントロールには、さまざまなソースからページに追加されるサーバー コントロールが含まれます。
重要 : |
---|
CatalogPart クラスから継承されたコントロールは、CatalogZoneBase クラスから派生したゾーンにしか配置できません。 |
PageCatalogPart | ページ上で閉じられていて、ユーザーによって再度開く (ページに再度追加する) ことができるコントロールが格納されます。この種類のカタログのコントロールは、既に別のソースからページに追加された後、ユーザーによって閉じられたコントロールです。 |
DeclarativeCatalogPart | Web ページのマークアップ内の CatalogZoneBase ゾーンに格納されている DeclarativeCatalogPart コントロール内で宣言されたサーバー コントロールが格納されます。 |
ImportCatalogPart | コントロールを Web ページに追加できるように、ユーザーがコントロールの定義ファイル (状態情報を含んだスキーマで定義された XML ファイル) をカタログにアップロードするためのユーザー インターフェイス (UI: User Interface) が用意されています。コントロール自体はこの種類のカタログには表示されません。カタログは、単に、外部コントロールの定義ファイルにアクセスして、それらのコントロールをページに追加できるようにするためのしくみです。 |
CatalogPart クラスは、基本 Part クラスから継承されるため、Web パーツ コントロール セットの他のパーツ コントロールと共通の動作を備えています。また、このクラスは、カタログ機能に役立ついくつかのメンバを追加します。DisplayTitle プロパティは、現在コントロールのタイトルとして表示されている実際の文字列を取得します。このプロパティの値は、Title プロパティの値か、またはそのプロパティに値が割り当てられていない場合には .NET Framework によって提供される既定値と同じにできます。WebPartManager プロパティおよび Zone プロパティによって、CatalogPart コントロールの有効期間を制御する 2 つの基本的なオブジェクトである WebPartManager コントロールおよび CatalogPart コントロールを含んだ CatalogZoneBase ゾーンのそれぞれに簡単にアクセスできます。
CatalogPart クラスには、いくつかのメソッドも含まれています。GetAvailableWebPartDescriptions メソッドは、継承先によって実装される必要がある抽象メソッドとして宣言されます。このメソッドは、カタログ内のコントロールの説明のコレクションを返します。関連するメソッドである GetWebPart も抽象メソッドとして宣言され、継承先によって実装される必要があります。このメソッドは、パラメータとしてメソッドに渡される説明に基づいて、WebPart コントロールのインスタンスを返します。
継承時の注意 CatalogPart クラスは抽象型であるため、Web ページ上では直接使用できません。Web パーツ コントロール セットには、基本クラスから派生し Web パーツ ページ上で使用できる 3 つの CatalogPart コントロールが用意されています。これらのコントロールについては、このトピックの「解説」の表を参照してください。これらのコントロールでは、Web サーバー コントロールのカタログを作成するために必要となるほとんどの機能が用意されています。ただし、特殊な要件がある場合には、カスタム CatalogPart コントロールの開発が必要になります。たとえば、Web サービスを経由して、あるいは直接データベースからサーバー コントロールを利用できるようにするための特殊な種類の CatalogPart コントロールを作成できます。このためには、CatalogPart クラスから継承する必要があります。また、抽象 GetAvailableWebPartDescriptions メソッドおよび抽象 GetWebPart メソッドをオーバーライドして、WebPart または他のサーバー コントロールとそれらの説明を返す実装を提供する必要もあります。さらに、各種のサーバー コントロールをデータベースや Web サービスから読み込むためのメソッドも必要になります。CatalogPart クラスの使用方法を示すコード例を次に示します。このクラスは抽象型であるため、コードから直接使用できません。ただし、これとは別の例として、Web パーツ コントロール セットで提供される 3 つの派生 CatalogPart コントロールの一般的な使用方法を次のコード例に示します。
-
Web パーツ コントロール セットで提供される 3 つの CatalogPart コントロール (すべて Web ページ上の CatalogZone の中で宣言されます) を含んだ Web ページ。このページには、ゾーンで宣言される AdRotator コントロールと共に、WebPartZone も含まれます。
コード例の最初の部分は、Web ページ上の表示モードをユーザーが変更できるようにするユーザー コントロールです。このコードは、(使用する言語に応じて) Displaymodemenuscs.ascx または Displaymodemenuvb.ascx という名前のファイルに保存する必要があります。表示モードの詳細、およびこのコントロールのソース コードの説明については、「チュートリアル : Web パーツ ページでの表示モードの変更」を参照してください。
<%@ control language="vb" classname="DisplayModeMenuVB"%> <script runat="server"> ' Use a field to reference the current WebPartManager. Dim _manager As WebPartManager Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) AddHandler Page.InitComplete, AddressOf InitComplete End Sub Sub InitComplete(ByVal sender As Object, ByVal e As System.EventArgs) _manager = WebPartManager.GetCurrentWebPartManager(Page) Dim browseModeName As String = WebPartManager.BrowseDisplayMode.Name ' Fill the dropdown with the names of supported display modes. Dim mode As WebPartDisplayMode For Each mode In _manager.SupportedDisplayModes Dim modeName As String = mode.Name ' Make sure a mode is enabled before adding it. If mode.IsEnabled(_manager) Then Dim item As New ListItem(modeName, modeName) DisplayModeDropdown.Items.Add(item) End If Next mode ' If shared scope is allowed for this user, display the scope-switching ' UI and select the appropriate radio button for the current user scope. If _manager.Personalization.CanEnterSharedScope Then Panel2.Visible = True If _manager.Personalization.Scope = PersonalizationScope.User Then RadioButton1.Checked = True Else RadioButton2.Checked = True End If End If End Sub ' Change the page to the selected display mode. Sub DisplayModeDropdown_SelectedIndexChanged(ByVal sender As Object, _ ByVal e As EventArgs) Dim selectedMode As String = DisplayModeDropdown.SelectedValue Dim mode As WebPartDisplayMode = _ _manager.SupportedDisplayModes(selectedMode) If Not (mode Is Nothing) Then _manager.DisplayMode = mode End If End Sub ' Set the selected item equal to the current display mode. Sub Page_PreRender(ByVal sender As Object, ByVal e As EventArgs) Dim items As ListItemCollection = DisplayModeDropdown.Items Dim selectedIndex As Integer = _ items.IndexOf(items.FindByText(_manager.DisplayMode.Name)) DisplayModeDropdown.SelectedIndex = selectedIndex End Sub ' Reset all of a user's personalization data for the page. Protected Sub LinkButton1_Click(ByVal sender As Object, _ ByVal e As EventArgs) _manager.Personalization.ResetPersonalizationState() End Sub ' If not in User personalization scope, toggle into it. Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, _ ByVal e As EventArgs) If _manager.Personalization.Scope = PersonalizationScope.Shared Then _manager.Personalization.ToggleScope() End If End Sub ' If not in Shared scope, and if user is allowed, toggle the scope. Protected Sub RadioButton2_CheckedChanged(ByVal sender As Object, _ ByVal e As EventArgs) If _manager.Personalization.CanEnterSharedScope AndAlso _ _manager.Personalization.Scope = PersonalizationScope.User Then _manager.Personalization.ToggleScope() End If End Sub </script> <div> <asp:Panel ID="Panel1" runat="server" Borderwidth="1" Width="230" BackColor="lightgray" Font-Names="Verdana, Arial, Sans Serif" > <asp:Label ID="Label1" runat="server" Text=" Display Mode" Font-Bold="true" Font-Size="8" Width="120" /> <asp:DropDownList ID="DisplayModeDropdown" runat="server" AutoPostBack="true" Width="120" OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" /> <asp:LinkButton ID="LinkButton1" runat="server" Text="Reset User State" ToolTip="Reset the current user's personalization data for the page." Font-Size="8" OnClick="LinkButton1_Click" /> <asp:Panel ID="Panel2" runat="server" GroupingText="Personalization Scope" Font-Bold="true" Font-Size="8" Visible="false" > <asp:RadioButton ID="RadioButton1" runat="server" Text="User" AutoPostBack="true" GroupName="Scope" OnCheckedChanged="RadioButton1_CheckedChanged" /> <asp:RadioButton ID="RadioButton2" runat="server" Text="Shared" AutoPostBack="true" GroupName="Scope" OnCheckedChanged="RadioButton2_CheckedChanged" /> </asp:Panel> </asp:Panel> </div>
<%@ control language="C#" classname="DisplayModeMenuCS"%> <script runat="server"> // Use a field to reference the current WebPartManager. WebPartManager _manager; void Page_Init(object sender, EventArgs e) { Page.InitComplete += new EventHandler(InitComplete); } void InitComplete(object sender, System.EventArgs e) { _manager = WebPartManager.GetCurrentWebPartManager(Page); String browseModeName = WebPartManager.BrowseDisplayMode.Name; // Fill the dropdown with the names of supported display modes. foreach (WebPartDisplayMode mode in _manager.SupportedDisplayModes) { String modeName = mode.Name; // Make sure a mode is enabled before adding it. if (mode.IsEnabled(_manager)) { ListItem item = new ListItem(modeName, modeName); DisplayModeDropdown.Items.Add(item); } } // If shared scope is allowed for this user, display the scope-switching // UI and select the appropriate radio button for the current user scope. if (_manager.Personalization.CanEnterSharedScope) { Panel2.Visible = true; if (_manager.Personalization.Scope == PersonalizationScope.User) RadioButton1.Checked = true; else RadioButton2.Checked = true; } } // Change the page to the selected display mode. void DisplayModeDropdown_SelectedIndexChanged(object sender, EventArgs e) { String selectedMode = DisplayModeDropdown.SelectedValue; WebPartDisplayMode mode = _manager.SupportedDisplayModes[selectedMode]; if (mode != null) _manager.DisplayMode = mode; } // Set the selected item equal to the current display mode. void Page_PreRender(object sender, EventArgs e) { ListItemCollection items = DisplayModeDropdown.Items; int selectedIndex = items.IndexOf(items.FindByText(_manager.DisplayMode.Name)); DisplayModeDropdown.SelectedIndex = selectedIndex; } // Reset all of a user's personalization data for the page. protected void LinkButton1_Click(object sender, EventArgs e) { _manager.Personalization.ResetPersonalizationState(); } // If not in User personalization scope, toggle into it. protected void RadioButton1_CheckedChanged(object sender, EventArgs e) { if (_manager.Personalization.Scope == PersonalizationScope.Shared) _manager.Personalization.ToggleScope(); } // If not in Shared scope, and if user is allowed, toggle the scope. protected void RadioButton2_CheckedChanged(object sender, EventArgs e) { if (_manager.Personalization.CanEnterSharedScope && _manager.Personalization.Scope == PersonalizationScope.User) _manager.Personalization.ToggleScope(); } </script> <div> <asp:Panel ID="Panel1" runat="server" Borderwidth="1" Width="230" BackColor="lightgray" Font-Names="Verdana, Arial, Sans Serif" > <asp:Label ID="Label1" runat="server" Text=" Display Mode" Font-Bold="true" Font-Size="8" Width="120" /> <asp:DropDownList ID="DisplayModeDropdown" runat="server" AutoPostBack="true" Width="120" OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" /> <asp:LinkButton ID="LinkButton1" runat="server" Text="Reset User State" ToolTip="Reset the current user's personalization data for the page." Font-Size="8" OnClick="LinkButton1_Click" /> <asp:Panel ID="Panel2" runat="server" GroupingText="Personalization Scope" Font-Bold="true" Font-Size="8" Visible="false" > <asp:RadioButton ID="RadioButton1" runat="server" Text="User" AutoPostBack="true" GroupName="Scope" OnCheckedChanged="RadioButton1_CheckedChanged" /> <asp:RadioButton ID="RadioButton2" runat="server" Text="Shared" AutoPostBack="true" GroupName="Scope" OnCheckedChanged="RadioButton2_CheckedChanged" /> </asp:Panel> </asp:Panel> </div>
コード例の 2 番目の部分は Web ページです。このページには CatalogZone コントロールが含まれ、子の <zonetemplate> 要素の中で、Web パーツ コントロール セットと共に提供される 3 つそれぞれの CatalogPart コントロールが宣言されます。いくつかの属性が DeclarativeCatalogPart コントロールに設定され、ページの <script> セクションでも、そのコントロールの属性がプログラムによってアクセスされます。このコントロールには、ユーザーがページに追加できる子コントロールとして、標準の Calendar コントロールが 1 つ含まれます。CatalogZone コントロールおよび CatalogPart コントロールは、ユーザーがページをカタログ表示モードに切り替えたときにだけ表示されます。
このページには WebPartZone コントロールも含まれており、その子の <zonetemplate> 要素内には、XML ファイルからの交互に切り替わるメッセージを表示する AdRotator コントロールがあります。ページが表示されているときに、ユーザーが閉じる動詞をクリックしてコントロールを閉じた場合、そのコントロールはページ カタログに追加されます。ユーザーは、ページをカタログ表示モードに切り替え、PageCatalogPart コントロールにアクセスして、閉じたコントロールを追加して元に戻すことにより、コントロールを再度開くことができます。
<%@ Page Language="vb" %> <%@ register TagPrefix="uc1" TagName="DisplayModeMenuVB" Src="DisplayModeMenuVB.ascx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Protected Sub Button1_Click(ByVal sender As Object, _ ByVal e As EventArgs) Label1.Text = "<h3>DeclarativeCatalogPart Property Values</h3>" & _ "Display Title: " & DeclarativeCatalogPart1.DisplayTitle & _ "<br />" & _ "Description: " & DeclarativeCatalogPart1.Description & _ "<br />" & _ "Chrome type: " & DeclarativeCatalogPart1.ChromeType.ToString() End Sub Protected Sub WebPartManager1_DisplayModeChanged(ByVal sender _ As Object, ByVal e As WebPartDisplayModeEventArgs) Label1.Text = String.Empty If WebPartManager1.DisplayMode _ Is WebPartManager.CatalogDisplayMode Then Button1.Visible = True Else Button1.Visible = False End If End Sub </script> <html > <head runat="server"> <title>CatalogPart Samples</title> </head> <body> <form id="form1" runat="server"> <asp:WebPartManager ID="WebPartManager1" runat="server" OnDisplayModeChanged="WebPartManager1_DisplayModeChanged" /> <uc1:DisplayModeMenuVB ID="DisplayModeMenu1" runat="server" /> <asp:WebPartZone ID="WebPartZone1" runat="server"> <ZoneTemplate> <asp:AdRotator ID="AdRotator1" runat="server" AdType="Banner" AdvertisementFile="~/quotes.xml" Title="Favorite Quotes" /> </ZoneTemplate> </asp:WebPartZone> <asp:CatalogZone ID="CatalogZone1" runat="server"> <ZoneTemplate> <asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" runat="server" Title="Controls to Add" ChromeType="TitleOnly" Description="Provides a list of controls that users can add to the page."> <WebPartsTemplate> <asp:Calendar ID="Calendar1" runat="server" Title="My Calendar" /> </WebPartsTemplate> </asp:DeclarativeCatalogPart> <asp:PageCatalogPart ID="PageCatalogPart1" runat="server" /> <asp:importcatalogpart id="ImportCatalogPart1" runat="server" /> </ZoneTemplate> </asp:CatalogZone> <hr /> <asp:Button ID="Button1" runat="server" Text="Display DeclarativeCatalogPart Properties" OnClick="Button1_Click" Visible="false"/> <br /> <asp:Label ID="Label1" runat="server" Text="" /> </form> </body> </html>
<%@ Page Language="C#" %> <%@ register TagPrefix="uc1" TagName="DisplayModeMenuCS" Src="DisplayModeMenuCS.ascx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Button1_Click(object sender, EventArgs e) { Label1.Text = "<h3>DeclarativeCatalogPart Property Values</h3>" + "Display Title: " + DeclarativeCatalogPart1.DisplayTitle + "<br />" + "Description: " + DeclarativeCatalogPart1.Description + "<br />" + "Chrome type: " + DeclarativeCatalogPart1.ChromeType.ToString(); } protected void WebPartManager1_DisplayModeChanged(object sender, WebPartDisplayModeEventArgs e) { Label1.Text = String.Empty; if (WebPartManager1.DisplayMode == WebPartManager.CatalogDisplayMode) Button1.Visible = true; else Button1.Visible = false; } </script> <html > <head runat="server"> <title>CatalogPart Samples</title> </head> <body> <form id="form1" runat="server"> <asp:WebPartManager ID="WebPartManager1" runat="server" OnDisplayModeChanged="WebPartManager1_DisplayModeChanged" /> <uc1:DisplayModeMenuCS ID="DisplayModeMenu1" runat="server" /> <asp:WebPartZone ID="WebPartZone1" runat="server"> <ZoneTemplate> <asp:AdRotator ID="AdRotator1" runat="server" AdType="Banner" AdvertisementFile="~/quotes.xml" Title="Favorite Quotes" /> </ZoneTemplate> </asp:WebPartZone> <asp:CatalogZone ID="CatalogZone1" runat="server"> <ZoneTemplate> <asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" runat="server" Title="Controls to Add" ChromeType="TitleOnly" Description="Provides a list of controls that users can add to the page."> <WebPartsTemplate> <asp:Calendar ID="Calendar1" runat="server" Title="My Calendar" /> </WebPartsTemplate> </asp:DeclarativeCatalogPart> <asp:PageCatalogPart ID="PageCatalogPart1" runat="server" /> <asp:importcatalogpart id="ImportCatalogPart1" runat="server" /> </ZoneTemplate> </asp:CatalogZone> <hr /> <asp:Button ID="Button1" runat="server" Text="Display DeclarativeCatalogPart Properties" OnClick="Button1_Click" Visible="false"/> <br /> <asp:Label ID="Label1" runat="server" Text="" /> </form> </body> </html>
コード例の 4 番目の部分は XML ファイルです。このファイルは、ページ上で宣言される AdRotator コントロールのソース ファイルです。このファイルには、コントロールによって定期的に入れ替えられて表示される、お気に入りの引用文が含まれています。次の内容をテキスト エディタにコピーし、Quotes.xml という名前で保存してください。
<?xml version="1.0" encoding="utf-8" ?> <Advertisements> <Ad> <AlternateText> A stitch in time saves nine. </AlternateText> </Ad> <Ad> <AlternateText> A penny saved is a penny earned. </AlternateText> </Ad> </Advertisements>
コード例を実行するには、Web ページをブラウザに読み込みます。[表示モード] ドロップダウン コントロールで、[カタログ] を選択して、ページをカタログ表示モードに切り替えます。Calendar コントロールが含まれる DeclarativeCatalogPart コントロールに設定された各種のプロパティ値がどのように影響を及ぼすかに注意してください。Calendar コントロールの横のチェック ボックスをオンにし、[追加] をクリックするとそのコントロールをページに追加できます。[Display DeclarativeCatalogPart Properties] ボタンをクリックすると、そのコントロールのプロパティの値が表示されます。[閉じる] をクリックしてページを通常のブラウズ モードに戻し、ページに追加したコントロールを表示できます。ここで、AdRotator コントロールの動詞メニュー (タイトル バーの下向きの矢印で表されます) に移動し、[閉じる] をクリックします。コントロールは閉じられ、ページ カタログに追加されます。ページをカタログ表示モードに再度切り替え、[ページ カタログ] ハイパーリンクをクリックして、PageCatalogPart コントロールを表示します。AdRotator コントロールは、ここではそのタイトルである [Favorite Quotes] で参照されます。[Favorite Quotes] コントロールを選択し、[追加] ボタンをクリックして、コントロールをページに再び追加します。[閉じる] ボタンをクリックしてページをブラウズ モードに戻します。
メモ |
---|
ImportCatalogPart コントロールはこのページでも宣言されていますが、コントロールをインポートする方法を示すサンプルはより複雑になるため、このコード例ではコントロールの完全な使用方法は示されていません。実際に動作する例については、ImportCatalogPart クラスまたは CatalogZone クラスのクラス概要ドキュメントを参照してください。 |
System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.Panel
System.Web.UI.WebControls.WebParts.Part
System.Web.UI.WebControls.WebParts.CatalogPart
System.Web.UI.WebControls.WebParts.DeclarativeCatalogPart
System.Web.UI.WebControls.WebParts.ImportCatalogPart
System.Web.UI.WebControls.WebParts.PageCatalogPart
プラットフォーム
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
CatalogPart メンバ
System.Web.UI.WebControls.WebParts 名前空間
CatalogZoneBase
その他の技術情報
チュートリアル : カスタム サーバー コントロールの開発と使用
チュートリアル : Web パーツ ページでの表示モードの変更
ASP.NET Web パーツ ページ
- CatalogPart クラスのページへのリンク