WebPartManager クラスとは? わかりやすく解説

WebPartManager クラス

メモ : このクラスは、.NET Framework version 2.0新しく追加されたものです。

Web パーツ コントロールWeb パーツ機能、および Web ページ上で発生するイベントのすべてを管理するWeb パーツ コントロール セット中心的なクラスとして機能します

名前空間: System.Web.UI.WebControls.WebParts
アセンブリ: System.Web (system.web.dll 内)
構文構文

<BindableAttribute(False)> _
Public Class WebPartManager
    Inherits Control
    Implements INamingContainer, IPersonalizable
Dim instance As WebPartManager
[BindableAttribute(false)] 
public class WebPartManager : Control, INamingContainer,
 IPersonalizable
[BindableAttribute(false)] 
public ref class WebPartManager : public
 Control, INamingContainer, IPersonalizable
/** @attribute BindableAttribute(false) */ 
public class WebPartManager extends Control
 implements INamingContainer, IPersonalizable
BindableAttribute(false) 
public class WebPartManager extends
 Control implements INamingContainer, IPersonalizable
解説解説

WebPartManager コントロールは、Web パーツ アプリケーションハブまたはコントロール センターとして動作しますWebPartManager コントロールインスタンスは、Web パーツ コントロール使用するページごとに 1 つだけ存在している必要がありますWeb パーツ アプリケーション大部分要素同様にWebPartManager コントロール認証されユーザーでのみ動作します。さらに、その機能のほとんどすべてが、WebZone クラスから継承した Web パーツ ゾーン内に存在するサーバー コントロール動作します。これらのゾーン外部ページ上に存在するサーバー コントロールは、Web パーツ機能をほとんど持たないか、WebPartManager コントロールとの対話もほとんど実行できません。

WebPartManager コントロールは、ページ上の Web パーツ機能ハブとして、次の表に示すような種類タスク実行します

タスクカテゴリ

コントロール実行する操作

Web パーツ コントロール追跡

WebPartコントロール接続ゾーンなどを含めWeb パーツ機能提供するページ上にある多様なコントロール追跡します

Web パーツ コントロール追加および削除

ページ上の WebPart コントロール追加削除、および終了に関するメソッド提供します

接続管理

コントロール間の接続作成し接続監視およびその追加削除プロセス監視行います

コントロールページパーソナル化

ユーザーが、ページ上の別の位置コントロール移動したり、コントロール外観プロパティ、および動作編集するビュー起動したりできるようにします。それぞれのページユーザー固有のパーソナル化設定維持します。

さまざまなページ ビュー切り替え

ユーザーページ レイアウト変更コントロール編集など特定のタスク実行できるように、ページ特殊化されさまざまなビュー切り替えます

Web パーツ有効期間イベントの発生

コントロール追加移動接続、または削除を行うときなどの Web パーツ コントロール有効期間イベント対する処理を開発者が定義、発生、および有効化ます。

コントロールインポートおよびエクスポート有効化

WebPart コントロールプロパティの状態を格納する XML ストリームエクスポートし、ユーザーがそのファイルインポートして、他のページまたはサイト複合コントロール簡単にパーソナル化できるようにします。

WebPartManager クラスには、数多くプロパティ セットありますWebPartManager は他のコントロール追跡する役割を果たすために、Web パーツ コントロールまたはその他の特別な Web パーツ オブジェクトコレクション参照する数多くプロパティ備えてます。AvailableTransformers、Connections、Controls、DisplayModes、DynamicConnections、SupportedDisplayModes、WebParts、および Zones の各プロパティはすべて、WebPartManager コントロール追跡その他の管理タスクのために使用するコレクションです。

プロパティ別のグループには、Web パーツ アプリケーション発生する特定の状況使用するカスタマイズ可能な警告含まれます。これらのプロパティには、CloseProviderWarning、DeleteWarning、ExportSensitiveDataWarning などがあります

WebPartManager クラスは、多くWeb サーバー コントロール使用される基本継承プロパティ一部オーバーライドます。これらのプロパティには、EnableTheming、SkinID、Visible などがあります

最後にアプリケーション現在の状態アクセスする場合に役立つプロパティグループありますDisplayMode プロパティは、ページ現在の表示モード示します。EnableClientScript プロパティは、コントロールにおいてクライアント側スクリプト表示許可されているかどうか示します。これは、ユーザーブラウザさまざまな機能持たせたりスクリプト機能オフにする場合に有効です。Internals プロパティは、機能拡張使用される多く重要な Web パーツ メソッド対す呼び出しを含む、ユーティリティ クラス参照する場合役立ちます別個のクラス (WebPartManagerInternals クラス) のこれらのメソッド対す呼び出し隠ぺいすることにより、WebPartManager クラス固有の API簡潔になりますPersonalization プロパティは、ユーザーパーソナル化設定格納し、そのデータ永続的なストレージ保持するパーソナル化オブジェクトへのアクセス提供します。SelectedWebPart プロパティは、ページ上でユーザーまたはアプリケーションが現在選択している WebPart コントロール示します。System.Web.UI.WebControls.WebParts.IPersonalizable.IsDirty プロパティは、WebPart コントロールカスタムパーソナル化データ変更されたかどうかを示します

WebPartManager コントロールには、5 つ組み込み表示モード、つまり Web ページビュー用意されています。開発者は、WebZone クラスや ToolZone クラスなどの型を拡張してカスタム表示モード作成することにより、この機能拡張できます指定され表示モード対応する適切な種類コントロールページ上に存在する場合ユーザーページさまざまな表示モード切り替えることができます

標準表示モードは、WebPartManager クラスパブリック フィールド表されます。フィールドおよびそれらが参照する表示モード概要次の表に示します上で説明したように、ページ現在の表示モードは常に DisplayMode プロパティ参照されます。また、特定のページ上で使用可能な表示モードセットは、ページ上にあるゾーン種類指定してSupportedDisplayModes プロパティ格納されます。

また、WebPartManager コントロールには、Web パーツページおよびコントロール有効期間で重要となる多くイベント用意されています。これらのイベントにより、Web パーツ コントロール動作に対してプログラムによる精度の高い制御を行うことができます大部分メソッドWebPart コントロール (または WebPart コントロールとして動作できるように WebPartZoneBase ゾーン配置されその他のサーバー コントロールまたはユーザー コントロール) に直接関係します。ただし、ページの状態またはページ上の接続関係するイベントはわずかです。利用できるイベントとその目的次の表に示します

メモメモ

次の表における "コントロール" はすべて、ゾーン内に存在し実行時に GenericWebPart オブジェクトによってラップされる、WebPart コントロールまたはサーバー コントロール表します

イベント

説明

AuthorizeWebPart

コントロールページ追加する直前発生し、それが承認されていることを検証します。

ConnectionsActivated

ページ上のすべての接続アクティブになってから発生します

ConnectionsActivating

ページ上のすべての接続アクティブにするプロセス直前発生します

DisplayModeChanged

ページ現在の表示モード変更された後に発生します

DisplayModeChanging

ページ表示モード変更するプロセス直前発生します

SelectedWebPartChanged

コントロール選択キャンセルされた後に発生します

SelectedWebPartChanging

コントロール選択キャンセルするプロセス直前発生します

WebPartAdded

コントロールゾーン追加された後に発生します

WebPartAdding

コントロールゾーン追加するプロセス直前発生します

WebPartClosed

コントロール閉じられた (ページから削除された) 後に発生します

WebPartClosing

コントロール閉じプロセス直前発生します

WebPartDeleted

プログラムによって作成されたか、カタログから追加されダイナミック コントロールインスタンスが、完全に削除された後に発生します

WebPartDeleting

ダイナミック コントロール削除するプロセス直前発生します

WebPartMoved

コントロールがそのゾーン内で移動されるかまたは別のゾーン移動された後に発生します

WebPartMoving

コントロール移動するプロセス直前発生します

WebPartsConnected

接続用として選択され2 つコントロール接続確立された後に発生します

WebPartsConnecting

2 つコントロール接続するプロセス直前発生します

WebPartsDisconnected

接続され2 つコントロール接続解除された後に発生します

WebPartsDisconnecting

2 つコントロール接続解除するプロセス直前発生します

WebPartManager コントロールは、Web パーツ ページ管理するための多数メソッド備えてます。ここに示されていない数多く一連のメソッドに、OnEventName形式で名前が付けられメソッドあります通常、これらのメソッド関連するイベント発生させ、そのイベントに WebPartEventHandler 型のハンドラ提供します。これらのメソッド大部分は、開発者WebPartManager クラスから継承してオーバーライドできますまた、ページ開発者はこれらのメソッド関連付けられたイベントカスタム ハンドラ用意できます。たとえば、WebPartAdded イベント場合ページ開発者Web ページマークアップ<asp:webpartmanager> 要素OnWebPartAdded 属性追加し属性カスタム メソッド名を割り当てることにより、そのイベント用のカスタム ハンドリング用意できます。この属性は OnWebPartAdded メソッド対応しイベント処理のこの基本的なパターンは、大部分Web パーツ イベントおよびそれに関連するメソッドに対して有効です。

さらに、WebPartManager コントロールは、WebPart コントロール (および WebPart コントロールとして使用されるサーバー コントロールまたはユーザー コントロール) を管理するタスクだけを対象としたメソッド備えてます。これらのメソッドには、AddWebPart、AuthorizeWebPart、CloseWebPart、CopyWebPart、CreateWebPart、DeleteWebPart、DisconnectWebPart、BeginWebPartEditing、EndWebPartEditing、ExportWebPart、GetGenericWebPart、ImportWebPart、IsAuthorized、および MoveWebPart があります

その他の一連のメソッドは、接続特化してます。このメソッドには、ActivateConnections、BeginWebPartConnecting、CanConnectWebParts、ConnectWebParts、CreateAvailableTransformers、DisconnectWebPart、DisconnectWebParts、EndWebPartConnecting、GetConsumerConnectionPoints、GetProviderConnectionPoints などがあります

最後に一部WebPartManager メソッドは、パーソナル化機能特化してます。これらには、CreatePersonalization、LoadControlState、SaveCustomPersonalizationState、SetPersonalizationDirty、System.Web.UI.WebControls.WebParts.IPersonalizable.Load、System.Web.UI.WebControls.WebParts.IPersonalizable.Save、および SaveControlState があります

Internals プロパティ通じてアクセスできるその他の WebPartManager メソッド詳細については、WebPartManagerInternals クラスドキュメント参照してください

継承時の注意 WebPartManager コントロールは、拡張できるようにデザインされています。このコントロールWeb パーツ アプリケーションハブとして機能します。したがってWeb パーツ コントロール セット一部特定の型またはコントロール拡張する場合通常WebPartManager クラス拡張必要になります。これは、多く場合このクラスが、Web パーツ アプリケーションコンテキストカスタム型を機能させるために必要ないくつかのプロパティまたはメソッド備えているためです。Web パーツ参照ドキュメント (System.Web.UI.WebControls.WebParts のトピック参照) では、Web パーツ型の拡張方法説明するときに、しばしば WebPartManager クラス拡張するために必要な作業についても説明され拡張する方法コード例示されています。

使用例使用例

WebPartManager コントロールを、宣言使用する方法およびプログラム使用する方法両方コード例次に示します

コード例は、次の 4 つ部分から成ります

ユーザー コントロールには、ページ上にある Web パーツ コントロール指定すると、そのページ使用可能な表示モード表示されるドロップダウンリスト コントロールあります。このコード例Web ページでは、このユーザー コントロールページマークアップにある WebPartManager 要素のすぐ下に宣言されています。また、Web ページ先頭近くには、コントロール登録する Register ディレクティブあります表示モード詳細、およびこのコントロールソース コード説明については、「チュートリアル : 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="&nbsp;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="&nbsp;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>

Web ページ宣言マークアップには、ユーザー コントロールカスタム コントロール両方Register ディレクティブ含まれています。<asp:webpartmanager> 要素カスタム コントロール格納する <asp:webpartzone> 要素、および <asp:connectionszone> 要素ありますまた、ページには、WebPartManager コントロール接続関連するイベント処理するいくつかのインライン コード含まれコントロール接続および接続解除した場合のこのコード効果確認できます

<%@ Page Language="vb" %>
<%@ register TagPrefix="uc1" 
  TagName="DisplayModeMenuVB" 
  Src="DisplayModeMenuVB.ascx" %>
<%@ register tagprefix="aspSample" 
  Namespace="Samples.AspNet.VB.Controls"
 
  Assembly="ConnectionSampleVB"
 %>

<script runat="server">

  Protected Sub WebPartManager1_WebPartsConnected(
 _
    ByVal sender As Object,
 _
    ByVal e As System.Web.UI.WebControls.WebParts.WebPartConnectionsEventArgs)
    
    UpdateLabelData(WebPartManager1.WebParts.Count, _
      WebPartManager1.Connections.Count)
    
  End Sub

  Protected Sub WebPartManager1_WebPartsDisconnected(
 _
    ByVal sender As Object,
 _
    ByVal e As System.Web.UI.WebControls.WebParts.WebPartConnectionsEventArgs)
    
    UpdateLabelData(WebPartManager1.WebParts.Count, _
      WebPartManager1.Connections.Count)
    
  End Sub
  
  Private Sub UpdateLabelData(ByVal
 wpCount As Integer, _
    ByVal connCount As Integer)
    
    Label1.Text = "WebPart Control Count:  " &
 wpCount.ToString()
    Label2.Text = "Connections Count: " & connCount.ToString()
    
  End Sub
</script>

<html  >
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">
      <!-- Reference the WebPartManager control. -->
      <asp:WebPartManager ID="WebPartManager1"
 runat="server" OnWebPartsConnected="WebPartManager1_WebPartsConnected"
 OnWebPartsDisconnected="WebPartManager1_WebPartsDisconnected" />
    <div>
      <uc1:DisplayModeMenuVB ID="displaymode1"
 runat="server" />
      <!-- Reference consumer and provider controls in
 a zone. -->
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:ZipCodeWebPart ID="zip1" 
            runat="server" 
            Title="Zip Code Control"/>
          <aspSample:WeatherWebPart ID="weather1"
 
            runat="server" 
            Title="Weather Control" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <asp:Label ID="Label1" runat="server"
 Text=""></asp:Label>
      <br />
      <asp:Label ID="Label2" runat="server"
 Text=""></asp:Label>
      <!-- Add a ConnectionsZone so users can connect controls. -->
      <asp:ConnectionsZone ID="ConnectionsZone1"
 runat="server" />
    </div>
    </form>
</body>
</html>
<%@ Page Language="C#" %>
<%@ register TagPrefix="uc1" 
  TagName="DisplayModeMenuCS" 
  Src="DisplayModeMenuCS.ascx" %>
<%@ register tagprefix="aspSample" 
  Namespace="Samples.AspNet.CS.Controls" 
  Assembly="ConnectionSampleCS" %>

<script runat="server">
 
  private void UpdateLabelData(int
 wpCount, int connCount)
  {
    Label1.Text = "WebPart Control Count:  " + wpCount.ToString();
    Label2.Text = "Connections Count: " + connCount.ToString();
  }

  protected void WebPartManager1_WebPartsConnected(object
 sender, WebPartConnectionsEventArgs e)
  {
    UpdateLabelData(WebPartManager1.WebParts.Count,
      WebPartManager1.Connections.Count);
  }

  protected void WebPartManager1_WebPartsDisconnected(object
 sender, WebPartConnectionsEventArgs e)
  {
    UpdateLabelData(WebPartManager1.WebParts.Count,
      WebPartManager1.Connections.Count);
  }
  
</script>

<html  >
<head id="Head1" runat="server">
</head>
<body>
    <form id="form1" runat="server">
      <!-- Reference the WebPartManager control. -->
      <asp:WebPartManager ID="WebPartManager1" runat="server"
  
        OnWebPartsConnected="WebPartManager1_WebPartsConnected" 
        OnWebPartsDisconnected="WebPartManager1_WebPartsDisconnected" />
    <div>
      <uc1:DisplayModeMenuCS ID="displaymode1" runat="server"
 />
      <!-- Reference consumer and provider controls in a zone.
 -->
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:ZipCodeWebPart ID="zip1" 
            runat="server" 
            Title="Zip Code Control"/>
          <aspSample:WeatherWebPart ID="weather1" 
            runat="server" 
            Title="Weather Control" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
      <br />
      <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
      <!-- Add a ConnectionsZone so users can connect controls. -->
      <asp:ConnectionsZone ID="ConnectionsZone1" runat="server"
 />
    </div>
    </form>
</body>
</html>

コード例3 番目の部分は、コントロールソース コードです。IZipCode という名前のインターフェイスがあり、このインターフェイスZipCodeWebPart クラス実装されています。このクラスには、プロバイダとして動作する ProvideIZipCode という名前の特別なコールバック メソッドありますまた、WeatherWebPart という名前のその他の型が、GetIZipCode という名前の特別なメソッド実装されています。このメソッドにより、このコントロールは他のコントロールコンシューマとして動作できます

コード例実行するためには、このソース コードコンパイルする必要があります。それを明示的にコンパイルし、コンパイル済みアセンブリWeb サイトBin フォルダまたはグローバル アセンブリ キャッシュ配置できますサイトの App_Code フォルダソース コード配置し実行時動的にコンパイルすることもできます。このコード例は、ソースアセンブリコンパイルしてあり、Web ページRegister ディレクティブがそのアセンブリ名参照していることを前提にしています。コンパイル方法を示すチュートリアルについては、「チュートリアル : カスタム サーバー コントロール開発と使用」を参照してください

Imports System
Imports System.Web
Imports System.Web.Security
Imports System.Security.Permissions
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 Interface IZipCode

    Property ZipCode() As String

  End Interface

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class ZipCodeWebPart
    Inherits WebPart
    Implements IZipCode
    Private zipCodeText As String
 = String.Empty
    Private input As TextBox
    Private send As Button

    Public Sub New()
    End Sub

    ' Make the implemented property personalizable to save 
    ' the Zip Code between browser sessions.
    <Personalizable()> _
    Public Property ZipCode() As
 String _
      Implements IZipCode.ZipCode

      Get
        Return zipCodeText
      End Get
      Set(ByVal value As
 String)
        zipCodeText = value
      End Set
    End Property

    ' This is the callback method that returns the provider.
    <ConnectionProvider("Zip Code")> _
    Public Function ProvideIZipCode() As
 IZipCode
      Return Me
    End Function


    Protected Overrides Sub
 CreateChildControls()
      Controls.Clear()
      input = New TextBox()
      Me.Controls.Add(input)
      send = New Button()
      send.Text = "Enter 5-digit Zip Code"
      AddHandler send.Click, AddressOf Me.submit_Click
      Me.Controls.Add(send)

    End Sub


    Private Sub submit_Click(ByVal
 sender As Object, _
      ByVal e As EventArgs)

      If input.Text <> String.Empty Then
        zipCodeText = Page.Server.HtmlEncode(input.Text)
        input.Text = String.Empty
      End If

    End Sub

  End Class

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class WeatherWebPart
    Inherits WebPart
    Private _provider As IZipCode
    Private _zipSearch As String
    Private DisplayContent As Label

    ' This method is identified by the ConnectionConsumer 
    ' attribute, and is the mechanism for connecting with 
    ' the provider. 
    <ConnectionConsumer("Zip Code")> _
    Public Sub GetIZipCode(ByVal
 Provider As IZipCode)
      _provider = Provider
    End Sub


    Protected Overrides Sub
 OnPreRender(ByVal e As EventArgs)
      EnsureChildControls()

      If Not (Me._provider
 Is Nothing) Then
        _zipSearch = _provider.ZipCode.Trim()
    DisplayContent.Text = "My Zip Code is:  " + _zipSearch
      End If

    End Sub 'OnPreRender

    Protected Overrides Sub
 CreateChildControls()
      Controls.Clear()
      DisplayContent = New Label()
      Me.Controls.Add(DisplayContent)

    End Sub

  End Class

End Namespace
namespace Samples.AspNet.CS.Controls
{
  using System;
  using System.Web;
  using System.Web.Security;
  using System.Security.Permissions;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;

  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public interface IZipCode
  {
    string ZipCode { get; set;}
  }

  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class ZipCodeWebPart : WebPart, IZipCode
  {
    string zipCodeText = String.Empty;
    TextBox input;
    Button send;

    public ZipCodeWebPart()
    {
    }

    // Make the implemented property personalizable to save 
    // the Zip Code between browser sessions.
    [Personalizable()]
    public virtual string ZipCode
    {
      get { return zipCodeText; }
      set { zipCodeText = value; }
    }

    // This is the callback method that returns the provider.
    [ConnectionProvider("Zip Code")]
    public IZipCode ProvideIZipCode()
    {
      return this;
    }

    protected override void CreateChildControls()
    {
      Controls.Clear();
      input = new TextBox();
      this.Controls.Add(input);
      send = new Button();
      send.Text = "Enter 5-digit Zip Code";
      send.Click += new EventHandler(this.submit_Click);
      this.Controls.Add(send);
    }

    private void submit_Click(object sender,
 EventArgs e)
    {
      if (input.Text != String.Empty)
      {
        zipCodeText = Page.Server.HtmlEncode(input.Text);
        input.Text = String.Empty;
      }
    }

  }

  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class WeatherWebPart : WebPart
  {
    private IZipCode _provider;
    string _zipSearch;
    Label DisplayContent;

    // This method is identified by the ConnectionConsumer 
    // attribute, and is the mechanism for connecting with 
    // the provider. 
    [ConnectionConsumer("Zip Code")]
    public void GetIZipCode(IZipCode Provider)
    {
      _provider = Provider;
    }
    
    protected override void OnPreRender(EventArgs
 e)
    {
      EnsureChildControls();

      if (this._provider != null)
      {
        _zipSearch = _provider.ZipCode.Trim();
        DisplayContent.Text = "My Zip Code is:  " + _zipSearch;
      }
    }

    protected override void CreateChildControls()
    {
      Controls.Clear();
      DisplayContent = new Label();
      this.Controls.Add(DisplayContent);
    }

  }
}

ブラウザWeb ページ読み込んだ後、[Display Mode] ドロップダウン リスト コントロールクリックし、[接続] を選択してページ接続モード切り替えます接続モードは、<asp:connectionszone> 要素使用してコントロールの間に接続作成できるようにします。接続モードで、[ZIP Code] コントロールタイトル バーにある下向き矢印クリックしてその動詞メニューアクティブにし、[接続] をクリックします。接続 UI表示されたら、[コンシューマへの接続作成] リンクをクリックします。ドロップダウン リスト コントロールを持つセル表示されます。ドロップダウン リストの [Weather Control] を選択し、[接続] をクリックして 2 つコントロール接続確立します。[閉じる] をクリックし、[Display Mode] ドロップダウン リスト使用してページ通常のブラウズ モード戻します郵便番号入力すると、コンシューマ コントロール入力した値により更新されます。ZipCode プロパティソース コードPersonalizable 属性マークしたため、このプロパティ値はブラウザ セッション全体わたって保持されユーザー入力した値が保存されます。より高度なコンシューマ コントロールでは、郵便番号情報取得し、この情報基づいて気象情報検索して、それをユーザー表示します

.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
   System.Web.UI.Control
    System.Web.UI.WebControls.WebParts.WebPartManager
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
WebPartManager メンバ
System.Web.UI.WebControls.WebParts 名前空間
WebPartManagerInternals
その他の技術情報
ASP.NET Web パーツ ページ


このページでは「.NET Framework クラス ライブラリ リファレンス」からWebPartManager クラスを検索した結果を表示しています。
Weblioに収録されているすべての辞書からWebPartManager クラスを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からWebPartManager クラス を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「WebPartManager クラス」の関連用語

WebPartManager クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



WebPartManager クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS