ProxyWebPartManager 类

定义

提供一种方法,使得在内容页面的关联母版页中声明了 WebPartManager 控件时,开发人员可在此内容页面中声明静态连接。

public ref class ProxyWebPartManager : System::Web::UI::Control
[System.ComponentModel.Bindable(false)]
public class ProxyWebPartManager : System.Web.UI.Control
[<System.ComponentModel.Bindable(false)>]
type ProxyWebPartManager = class
    inherit Control
Public Class ProxyWebPartManager
Inherits Control
继承
ProxyWebPartManager
属性

示例

下面的代码示例演示如何在使用母版页的应用程序中,使用 ProxyWebPartManager 类在内容页上声明静态连接。 该示例包含五个部分:

  • 一个用户控件,可用于更改页面上的 Web 部件显示模式。

  • 接口和两 WebPart 个控件的源代码,充当连接的提供程序和使用者。

  • 承载用户控件、内容页和 WebPartManager 应用程序控件的母版网页。

  • 承载一个控件、两个 ProxyWebPartManager 自定义 WebPart 控件和一个用于连接这两个控件的静态连接的内容网页。

  • 有关如何运行示例页的说明。

此代码示例的第一部分是用户控件,使用户能够更改网页上的显示模式。 将以下源代码保存到 .ascx 文件,为其提供分配给 Src 此用户控件的 指令的 Register 属性的文件名,该属性位于宿主母版页顶部附近。 有关此控件中的显示模式和源代码说明的详细信息,请参阅 演练:更改 Web 部件页上的显示模式

<%@ 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" 
      AssociatedControlID="DisplayModeDropdown"/>
    <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="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" 
      AssociatedControlID="DisplayModeDropdown"/>
    <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>

代码示例的第二部分是接口和控件的源代码。 源文件包含名为 的 IZipCode简单接口。 还有一个名为 WebPartZipCodeWebPart 的类,该类实现 接口并充当提供程序控件。 其 ProvideIZipCode 方法是实现接口的唯一成员的回调方法。 方法仅返回 接口的实例。 请注意,方法的元数据中标有属性 ConnectionProvider 。 这是将 方法标识为提供程序连接点的回调方法的机制。 另一 WebPart 个类名为 WeatherWebPart,它充当连接的使用者。 此类具有名为 GetZipCode 的方法,该方法从提供程序控件获取接口的 IZipCode 实例。 请注意,此方法被标记为使用者的连接点方法,其元数据中具有 属性 ConnectionConsumer

若要运行代码示例,必须编译此源代码。 可以显式编译它,并将生成的程序集放入网站的 Bin 文件夹或全局程序集缓存中。 或者,可以将源代码放在站点的“App_Code”文件夹中,并在运行时对其进行动态编译。 此代码示例使用动态编译。 有关演示如何编译的演练,请参阅 演练:开发和使用自定义 Web 服务器控件

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 Provider", "ZipCodeProvider")]
    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 (!string.IsNullOrEmpty(input.Text))
      {
        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 Consumer", "ZipCodeConsumer")]
    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);
    }
  }
}
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 Provider", "ZipCodeProvider")> _
    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 Consumer", "ZipCodeConsumer")> _
    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

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

    End Sub

  End Class

End Namespace

代码示例的第三部分是母版页。 应使用以下源代码并将其保存在文件中,将其命名为 MasterPageCS.master 或 MasterPageVB.master (,具体取决于) 使用的语言。 请注意,母版页包含用于 Register 注册用户控件的 指令,并在页面正文中引用用户控件本身。 母版页还声明用于此页和所有相关内容页的单个 <asp:webpartmanager> 元素。 最后,母版页具有一个 <asp: contentplaceholder> 元素,该元素声明插入内容页的页面中的点。

<%@ Master 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">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Master page with connections in content pages</title>
</head>
<body>
    <h2>Contoso, Ltd.</h2>
    <hr />
    <form id="form1" runat="server">
    <asp:webpartmanager runat="server" id="WebPartManager1" />
    <uc1:displaymodemenucs id="menu1" runat="server" />
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" 
          runat="server" />
    </div>
    </form>
</body>
</html>
<%@ Master 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">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Master page with connections in content pages</title>
</head>
<body>
    <h2>Contoso, Ltd.</h2>
    <hr />
    <form id="form1" runat="server">
    <asp:webpartmanager runat="server" id="WebPartManager1" />
    <uc1:displaymodemenuvb id="menu1" runat="server" />
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" 
          runat="server" />
    </div>
    </form>
</body>
</html>

代码示例的第四部分是内容页。 应复制以下源代码并将其保存在扩展名为 .aspx 的文件中。 请注意,其 Page 指令包含用于 MasterFile 引用母版页的属性。 此外,此页有一个 Register 指令,用于在包含参与连接的动态编译自定义 WebPart 控件的 App_Code 文件夹中注册文件。 在 <asp:content> 页面的标记中,有一个 <asp:proxywebpartmanager> 元素和一个子 <staticconnections> 元素,而子元素又有一个用于声明连接详细信息的子 <asp:webpartconnection> 元素。 在页面上的<script>标记中, Button1_Click 方法添加一些代码,用于访问母版页中的 main WebPartManager 控件和ProxyWebPartManager内容页中的 控件,并将其一些详细信息写入页面。

<%@ Page Language="C#" MasterPageFile="~/MasterPageCS.master" 
  Title="Connections Page" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.CS.Controls" %>

<script runat="server">

  protected void Button1_Click(object sender, EventArgs e)
  {
    StringBuilder lblText = new StringBuilder();
    
    if (Page.Master.FindControl("WebPartManager1") != null)
    {
      WebPartManager theMgr = 
        (WebPartManager)Page.Master.FindControl("WebPartManager1");
      lblText.Append("WebPartManager:  <br /><pre>" +
        "  Master page file is " + Page.MasterPageFile + "<br />" +
        "  ID is " + theMgr.ID + "<br />" +
        "  Connection count is " +
           theMgr.StaticConnections.Count.ToString() + "<br />" +
        "  WebParts count is " +
           theMgr.WebParts.Count.ToString() + "</pre><br />");
    }

    if (proxymgr1 != null)
    {
      lblText.Append("ProxyWebPartManager:  <br /><pre>" +
        "  Content page file is " + Request.Path + "<br />" +
        "  ID is " + proxymgr1.ID + "<br />" +
        "  Connection count is " +
           proxymgr1.StaticConnections.Count.ToString() + 
           "</pre><br />");
    }

    Literal1.Text = lblText.ToString();
    
  }
  
</script>

<asp:Content ID="Content1" Runat="Server" 
  ContentPlaceHolderID="ContentPlaceHolder1" >
 
  <asp:proxywebpartmanager id="proxymgr1" runat="server">
    <staticconnections>
      <asp:webpartconnection id="connection1" 
        consumerconnectionpointid="ZipCodeConsumer"
        consumerid="zipConsumer"
        providerconnectionpointid="ZipCodeProvider" 
        providerid="zipProvider" />
    </staticconnections>    
  </asp:proxywebpartmanager>

  <div>
  <asp:webpartzone id="zone1" runat="server">
    <zonetemplate>
      <aspsample:zipcodewebpart id="zipProvider" runat="server" 
        title="Zip Code Provider"  />
      <aspsample:weatherwebpart id="zipConsumer" runat="server" 
        title="Zip Code Consumer" />
    </zonetemplate>
  </asp:webpartzone>
  </div>
  
  <div>
  <asp:button id="Button1" runat="server" 
    text="WebPartManager Information" onclick="Button1_Click" />
  <br />
  
  </div>
  
  <asp:connectionszone id="ConnectionsZone1" runat="server" />
  <asp:literal id="Literal1" runat="server" />

</asp:Content>
<%@ Page Language="VB" MasterPageFile="~/MasterPageVB.master" 
  Title="Connections Page" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.VB.Controls" %>

<script runat="server">

  Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As EventArgs)

    Dim lblText As StringBuilder = New StringBuilder()

    If Not (Page.Master.FindControl("WebPartManager1") Is Nothing) Then
      Dim theMgr As WebPartManager = _
        CType(Page.Master.FindControl("WebPartManager1"), WebPartManager)
      lblText.Append("WebPartManager:  <br /><pre>" & _
        "  Master page file is " & Page.MasterPageFile & "<br />" & _
        "  ID is " & theMgr.ID & "<br />" & _
        "  Connection count is " & _
           theMgr.StaticConnections.Count.ToString() & "<br />" & _
        "  WebParts count is " & _
           theMgr.WebParts.Count.ToString() & "</pre><br />")
    End If

    If Not (proxymgr1 Is Nothing) Then
      lblText.Append("ProxyWebPartManager:  <br /><pre>" & _
        "  Content page file is " & Request.Path & "<br />" & _
        "  ID is " & proxymgr1.ID & "<br />" & _
        "  Connection count is " & _
           proxymgr1.StaticConnections.Count.ToString() & "</pre><br />")
    End If

    Literal1.Text = lblText.ToString()
    
  End Sub

</script>

<asp:Content ID="Content1" Runat="Server" 
  ContentPlaceHolderID="ContentPlaceHolder1" >

  <asp:proxywebpartmanager id="proxymgr1" runat="server">
    <staticconnections>
      <asp:webpartconnection id="connection1" 
        consumerconnectionpointid="ZipCodeConsumer"
        consumerid="zipConsumer"
        providerconnectionpointid="ZipCodeProvider" 
        providerid="zipProvider" />
    </staticconnections>    
  </asp:proxywebpartmanager>

  <div>
  <asp:webpartzone id="zone1" runat="server">
    <zonetemplate>
      <aspsample:zipcodewebpart id="zipProvider" runat="server" 
        title="Zip Code Provider"  />
      <aspsample:weatherwebpart id="zipConsumer" runat="server" 
        title="Zip Code Consumer" />
    </zonetemplate>
  </asp:webpartzone>
  </div>
  
  <div>
  <asp:button id="Button1" runat="server" 
    text="WebPartManager Information" onclick="Button1_Click" />
  <br />
  <asp:literal id="Literal1" runat="server" />
  </div>
  
  <asp:connectionszone id="ConnectionsZone1" runat="server" />
  
</asp:Content>

在浏览器中加载页面后,单击“WebPartManager 信息”按钮,并观察母版页中的控件和ProxyWebPartManager内容页中的控件的相关信息WebPartManager。 例如,请注意,它们各自属性中的计数相同,用于跟踪静态连接 (StaticConnections 属性) 。 另请注意,尽管 WebPartManager 控件具有跟踪 WebParts 其管理的控件数的属性 WebPartProxyWebPartManager 但控件没有此类属性,因为它的唯一用途是包含静态连接。

注解

当控件已在母版页中声明时WebPartManager,该ProxyWebPartManager控件适用于在内容页中声明静态连接的特定方案。

根据设计,使用 Web 部件控件的网页必须包含一个 (和一个管理页面上所有 Web 部件控件的 WebPartManager) 控件。 当 Web 部件应用程序使用母版页时,将控件放置在 WebPartManager 母版页中很常见,因为所有内容页在运行时都与母版页合并,并且单个 WebPartManager 控件将管理所有内容页中的所有 Web 部件控件。 但是,当开发人员想要在此类应用程序的内容页中声明静态连接时,他们似乎可能面临限制。 静态 Web 部件连接只能通过添加 <asp:webpartconnection> 元素作为 元素的 <staticconnections> 子元素来声明,该元素本身必须是 元素的 <asp:webpartmanager> 子元素。 但是,由于控件 WebPartManager 已在母版页中声明,并且是允许的 WebPartManager 控件,因此开发人员无法在内容页中声明其他 WebPartManager 控件以添加静态连接。

ProxyWebPartManager在此方案中, 控件将取代 WebPartManager 控件。 开发人员在其内容页中声明 <asp:proxywebpartmanager> 元素 <asp:webpartmanager> 而不是元素,然后可以将静态连接声明为子元素。 在运行时,控件中的 ProxyWebPartManager 连接只是添加到 StaticConnections 控件的集合中 WebPartManager ,并像对待任何其他连接一样。

ProxyWebPartManager由于 控件仅在此特定开发方案中使用,因此其功能比 WebPartManager 类更有限。 事实上,尽管 控件 ProxyWebPartManager 充当代理以包含内容页中控件的 WebPartManager 静态连接,但它不会继承自 WebPartManager 控件。 它直接从 Control 类继承,并且仅重写几个基成员。 、 EnableThemingVisibleSkinID 属性被重写并分配了阻止使用它们的值。 将重写其他继承的属性以调整其设计时行为,但除此之外,它们的行为与基本属性相同。 其中包括 ControlsClientID 属性。 最后, ProxyWebPartManager 类具有一个非继承属性。 属性 StaticConnections 返回其自己的静态连接集合 (ProxyWebPartConnectionCollection 对象) 。

对于方法, ProxyWebPartManager 类同样只重写少数方法,主要是为了限制其使用。 如果调用,则通过引发异常,使继承 Focus 的方法不可用。 方法 CreateControlCollection 始终返回一个空的控件集合,这会阻止它包含控件集合。 最后, OnInit 方法调用基方法,然后将 属性WebPartManager.StaticConnections引用StaticConnections的连接集合分配给 控件的 WebPartManager 属性。 这可以汇总所有内容页中声明的所有静态连接,并使它们成为母版页中控件维护 WebPartManager 的连接集合的一部分。

构造函数

ProxyWebPartManager()

初始化 ProxyWebPartManager 类的新实例。

属性

Adapter

获取控件的浏览器特定适配器。

(继承自 Control)
AppRelativeTemplateSourceDirectory

获取或设置包含该控件的 PageUserControl 对象的应用程序相对虚拟目录。

(继承自 Control)
BindingContainer

获取包含该控件的数据绑定的控件。

(继承自 Control)
ChildControlsCreated

获取一个值,该值指示是否已创建服务器控件的子控件。

(继承自 Control)
ClientID

获取由 ASP.NET 生成的 HTML 标记的控件 ID。

ClientIDMode

获取或设置用于生成 ClientID 属性值的算法。

(继承自 Control)
ClientIDSeparator

获取一个字符值,该值表示 ClientID 属性中使用的分隔符字符。

(继承自 Control)
Context

为当前 Web 请求获取与服务器控件关联的 HttpContext 对象。

(继承自 Control)
Controls

获取 ControlCollection 对象,该对象表示 UI 层次结构中的指定服务器控件的子控件。

DataItemContainer

如果命名容器实现 IDataItemContainer,则获取对命名容器的引用。

(继承自 Control)
DataKeysContainer

如果命名容器实现 IDataKeysControl,则获取对命名容器的引用。

(继承自 Control)
DesignMode

获取一个值,该值指示是否正在使用设计图面上的一个控件。

(继承自 Control)
EnableTheming

重写基属性,以防止使用主题。

EnableViewState

获取或设置一个值,该值指示服务器控件是否向发出请求的客户端保持自己的视图状态以及它所包含的任何子控件的视图状态。

(继承自 Control)
Events

获取控件的事件处理程序委托列表。 此属性为只读。

(继承自 Control)
HasChildViewState

获取一个值,该值指示当前服务器控件的子控件是否具有任何已保存的视图状态设置。

(继承自 Control)
ID

获取或设置分配给服务器控件的编程标识符。

(继承自 Control)
IdSeparator

获取用于分隔控件标识符的字符。

(继承自 Control)
IsChildControlStateCleared

获取一个值,该值指示该控件中包含的控件是否具有控件状态。

(继承自 Control)
IsTrackingViewState

获取一个值,用于指示服务器控件是否会将更改保存到其视图状态中。

(继承自 Control)
IsViewStateEnabled

获取一个值,该值指示是否为该控件启用了视图状态。

(继承自 Control)
LoadViewStateByID

获取一个值,该值指示控件是否通过 ID 而不是索引参与加载其视图状态。

(继承自 Control)
NamingContainer

获取对服务器控件的命名容器的引用,此引用创建唯一的命名空间,以区分具有相同 ID 属性值的服务器控件。

(继承自 Control)
Page

获取对包含服务器控件的 Page 实例的引用。

(继承自 Control)
Parent

获取对页 UI 层次结构中服务器控件的父控件的引用。

(继承自 Control)
RenderingCompatibility

获取一个值,该值指定呈现的 HTML 将与之兼容的 ASP.NET 版本。

(继承自 Control)
Site

获取容器信息,该容器在呈现于设计图面上时承载当前控件。

(继承自 Control)
SkinID

重写基属性,以防止对该基属性赋值。

StaticConnections

获取内容页上在 <asp:proxywebpartmanager> 元素中声明的静态连接的集合。

TemplateControl

获取或设置对包含该控件的模板的引用。

(继承自 Control)
TemplateSourceDirectory

获取包含当前服务器控件的 PageUserControl 的虚拟目录。

(继承自 Control)
UniqueID

获取服务器控件的唯一的、以分层形式限定的标识符。

(继承自 Control)
ValidateRequestMode

获取或设置指示控件是否检查来自浏览器的客户端输入是否具有潜在危险值的值。

(继承自 Control)
ViewState

获取状态信息的字典,这些信息使您可以在同一页的多个请求间保存和还原服务器控件的视图状态。

(继承自 Control)
ViewStateIgnoresCase

获取一个值,该值指示 StateBag 对象是否不区分大小写。

(继承自 Control)
ViewStateMode

获取或设置此控件的视图状态模式。

(继承自 Control)
Visible

重写基属性,以防止对该基属性赋值。

方法

AddedControl(Control, Int32)

在子控件添加到 Control 对象的 Controls 集合后调用。

(继承自 Control)
AddParsedSubObject(Object)

通知服务器控件,分析了一个元素(XML 或 HTML),并将该元素添加到服务器控件的 ControlCollection 对象中。

(继承自 Control)
ApplyStyleSheetSkin(Page)

将页样式表中定义的样式属性应用到控件。

(继承自 Control)
BeginRenderTracing(TextWriter, Object)

开始输出数据的设计时追踪。

(继承自 Control)
BuildProfileTree(String, Boolean)

收集有关服务器控件的信息并将该信息发送到 Trace 属性,在启用页的跟踪功能时将显示该属性。

(继承自 Control)
ClearCachedClientID()

将缓存的 ClientID 值设置为 null

(继承自 Control)
ClearChildControlState()

删除服务器控件的子控件的控件状态信息。

(继承自 Control)
ClearChildState()

删除服务器控件的所有子控件的视图状态和控件状态信息。

(继承自 Control)
ClearChildViewState()

删除服务器控件的所有子控件的视图状态信息。

(继承自 Control)
ClearEffectiveClientIDMode()

将当前控件实例和任何子控件的 ClientIDMode 属性设置为 Inherit

(继承自 Control)
CreateChildControls()

由 ASP.NET 页框架调用,以通知服务器控件在准备回发或呈现时使用基于撰写的实现来创建其所包含任何子控件。

(继承自 Control)
CreateControlCollection()

重写基属性,以阻止 ProxyWebPartManager 控件包含其他控件。

DataBind()

将数据源绑定到调用的服务器控件及其所有子控件。

(继承自 Control)
DataBind(Boolean)

将数据源绑定到调用的服务器控件及其所有子控件,同时可以选择引发 DataBinding 事件。

(继承自 Control)
DataBindChildren()

将数据源绑定到服务器控件的子控件。

(继承自 Control)
Dispose()

使服务器控件得以在从内存中释放之前执行最后的清理操作。

(继承自 Control)
EndRenderTracing(TextWriter, Object)

结束输出数据的设计时追踪。

(继承自 Control)
EnsureChildControls()

确定服务器控件是否包含子控件。 如果不包含,则创建子控件。

(继承自 Control)
EnsureID()

为尚未分配标识符的控件创建标识符。

(继承自 Control)
Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
FindControl(String)

在当前的命名容器中搜索带指定 id 参数的服务器控件。

(继承自 Control)
FindControl(String, Int32)

使用指定的 idpathOffset 参数(该参数有助于搜索)中指定的整数在当前命名容器中搜索服务器控件。 不应重写此版本的 FindControl 方法。

(继承自 Control)
Focus()

重写基方法,以防止该方法被调用。

GetDesignModeState()

获取控件的设计时数据。

(继承自 Control)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetRouteUrl(Object)

获取与一组路由参数对应的 URL。

(继承自 Control)
GetRouteUrl(RouteValueDictionary)

获取与一组路由参数对应的 URL。

(继承自 Control)
GetRouteUrl(String, Object)

获取与一组路由参数以及某个路由名称对应的 URL。

(继承自 Control)
GetRouteUrl(String, RouteValueDictionary)

获取与一组路由参数以及某个路由名称对应的 URL。

(继承自 Control)
GetType()

获取当前实例的 Type

(继承自 Object)
GetUniqueIDRelativeTo(Control)

返回指定控件的 UniqueID 属性的前缀部分。

(继承自 Control)
HasControls()

确定服务器控件是否包含任何子控件。

(继承自 Control)
HasEvents()

返回一个值,该值指示是否为控件或任何子控件注册事件。

(继承自 Control)
IsLiteralContent()

确定服务器控件是否只包含文字内容。

(继承自 Control)
LoadControlState(Object)

SaveControlState() 方法保存的上一个页请求还原控件状态信息。

(继承自 Control)
LoadViewState(Object)

从用 SaveViewState() 方法保存的上一个页面请求还原视图状态信息。

(继承自 Control)
MapPathSecure(String)

检索虚拟路径(绝对的或相对的)映射到的物理路径。

(继承自 Control)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
OnBubbleEvent(Object, EventArgs)

确定服务器控件的事件是否沿页的 UI 服务器控件层次结构向上传递。

(继承自 Control)
OnDataBinding(EventArgs)

引发 DataBinding 事件。

(继承自 Control)
OnInit(EventArgs)

引发 Init 事件并初始化控件。

OnLoad(EventArgs)

引发 Load 事件。

(继承自 Control)
OnPreRender(EventArgs)

引发 PreRender 事件。

(继承自 Control)
OnUnload(EventArgs)

引发 Unload 事件。

(继承自 Control)
OpenFile(String)

获取用于读取文件的 Stream

(继承自 Control)
RaiseBubbleEvent(Object, EventArgs)

将所有事件源及其信息分配给控件的父级。

(继承自 Control)
RemovedControl(Control)

Control 对象的 Controls 集合移除子控件后调用。

(继承自 Control)
Render(HtmlTextWriter)

将服务器控件内容发送到给定的 HtmlTextWriter 对象,该对象可编写将在客户端呈现的内容。

(继承自 Control)
RenderChildren(HtmlTextWriter)

将服务器控件子级的内容输出到提供的 HtmlTextWriter 对象,该对象可写入要在客户端上呈现的内容。

(继承自 Control)
RenderControl(HtmlTextWriter)

将服务器控件内容输出到所提供的 HtmlTextWriter 对象,如果启用了跟踪,则还将存储有关该控件的跟踪信息。

(继承自 Control)
RenderControl(HtmlTextWriter, ControlAdapter)

使用提供的 HtmlTextWriter 对象将服务器控件内容输出到提供的 ControlAdapter 对象。

(继承自 Control)
ResolveAdapter()

获取负责呈现指定控件的控件适配器。

(继承自 Control)
ResolveClientUrl(String)

获取浏览器可以使用的 URL。

(继承自 Control)
ResolveUrl(String)

将 URL 转换为在请求客户端可用的 URL。

(继承自 Control)
SaveControlState()

保存将页面回发到服务器之后发生的所有服务器控件状态更改。

(继承自 Control)
SaveViewState()

保存将页面回发到服务器之后发生的所有服务器控件视图状态更改。

(继承自 Control)
SetDesignModeState(IDictionary)

为控件设置设计时数据。

(继承自 Control)
SetRenderMethodDelegate(RenderMethod)

分配事件处理程序委托,以将服务器控件及其内容呈现到父控件中。

(继承自 Control)
SetTraceData(Object, Object)

使用跟踪数据键和跟踪数据值,为呈现数据的设计时追踪设置跟踪数据。

(继承自 Control)
SetTraceData(Object, Object, Object)

使用跟踪对象、跟踪数据键和跟踪数据值,为呈现数据的设计时追踪设置跟踪数据。

(继承自 Control)
ToString()

返回表示当前对象的字符串。

(继承自 Object)
TrackViewState()

导致跟踪服务器控件的视图状态的更改,以便这些更改可以存储到服务器控件的 StateBag 对象中。 通过 ViewState 属性可访问此对象。

(继承自 Control)

事件

DataBinding

当服务器控件绑定到数据源时发生。

(继承自 Control)
Disposed

当从内存释放服务器控件时发生,这是请求 ASP.NET 页时服务器控件生存期的最后阶段。

(继承自 Control)
Init

当服务器控件初始化时发生;初始化是控件生存期的第一步。

(继承自 Control)
Load

当服务器控件加载到 Page 对象中时发生。

(继承自 Control)
PreRender

在加载 Control 对象之后、呈现之前发生。

(继承自 Control)
Unload

当服务器控件从内存中卸载时发生。

(继承自 Control)

显式接口实现

IControlBuilderAccessor.ControlBuilder

有关此成员的说明,请参见 ControlBuilder

(继承自 Control)
IControlDesignerAccessor.GetDesignModeState()

有关此成员的说明,请参见 GetDesignModeState()

(继承自 Control)
IControlDesignerAccessor.SetDesignModeState(IDictionary)

有关此成员的说明,请参见 SetDesignModeState(IDictionary)

(继承自 Control)
IControlDesignerAccessor.SetOwnerControl(Control)

有关此成员的说明,请参见 SetOwnerControl(Control)

(继承自 Control)
IControlDesignerAccessor.UserData

有关此成员的说明,请参见 UserData

(继承自 Control)
IDataBindingsAccessor.DataBindings

有关此成员的说明,请参见 DataBindings

(继承自 Control)
IDataBindingsAccessor.HasDataBindings

有关此成员的说明,请参见 HasDataBindings

(继承自 Control)
IExpressionsAccessor.Expressions

有关此成员的说明,请参见 Expressions

(继承自 Control)
IExpressionsAccessor.HasExpressions

有关此成员的说明,请参见 HasExpressions

(继承自 Control)
IParserAccessor.AddParsedSubObject(Object)

有关此成员的说明,请参见 AddParsedSubObject(Object)

(继承自 Control)

扩展方法

FindDataSourceControl(Control)

返回与指定控件的数据控件关联的数据源。

FindFieldTemplate(Control, String)

返回指定控件的命名容器中指定列的字段模板。

FindMetaTable(Control)

返回包含数据控件的元表对象。

适用于

另请参阅