ConsumerConnectionPoint 类

定义

定义连接点对象,该对象允许服务器控件作为使用者,以建立与提供者的连接。

public ref class ConsumerConnectionPoint : System::Web::UI::WebControls::WebParts::ConnectionPoint
public class ConsumerConnectionPoint : System.Web.UI.WebControls.WebParts.ConnectionPoint
type ConsumerConnectionPoint = class
    inherit ConnectionPoint
Public Class ConsumerConnectionPoint
Inherits ConnectionPoint
继承
ConsumerConnectionPoint

示例

下面的代码示例演示了在每种情况下使用使用者连接点以声明方式、编程方式或通过 UI 创建连接的简单方法。

该示例包含四个部分:

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

  • 接口和两 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=" 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=" 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 的类,该类实现 接口并充当提供程序控件。 另一 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

代码示例的第三部分是网页。 顶部附近是 Register 用于注册构成连接的自定义控件的指令,以及允许用户更改页面上显示模式的用户控件。 连接本身是在页面上的 元素内 <staticconnections> 以声明方式创建的。 这演示了创建连接的一种方法 -- 注意 ConsumerConnectionPointID 元素中的 <asp:webpartconnection> 属性。 还可以以编程方式创建连接;用于执行此操作的代码位于 方法中 Button1_Click 。 在这种情况下,将创建一个 ConsumerConnectionPoint 对象,然后将其传递给创建实际连接的方法。 无论是以声明方式还是以编程方式创建连接,都必须始终为提供程序和使用者指定连接点。 方法 Button2_Click 访问 ConnectionPoint 提供程序和使用者的对象,并将其一些属性值写入页面中的标签。

<%@ Page Language="C#" %>
<%@ register tagprefix="uc1" 
    tagname="DisplayModeMenuCS"
    src="~/displaymodemenucs.ascx" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.CS.Controls" %>
    
<!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)
  {
    ProviderConnectionPoint provPoint =
      mgr.GetProviderConnectionPoints(zip1)["ZipCodeProvider"];
    ConsumerConnectionPoint connPoint =
      mgr.GetConsumerConnectionPoints(weather1)["ZipCodeConsumer"];
      
    if(mgr.CanConnectWebParts(zip1, provPoint, weather1, connPoint))
      mgr.ConnectWebParts(zip1, provPoint, weather1, connPoint);
  
  }  
  protected void Button2_Click(object sender, EventArgs e)
  {
    WebPartConnection conn = mgr.Connections[0];
    
    lblConn.Text = "<h2>Connection Point Details</h2>" + 
       "<h3>Provider Connection Point</h3>" + 
       "  Display name: " + conn.ProviderConnectionPoint.DisplayName + 
       "<br />" + 
       "  ID: " + conn.ProviderConnectionPoint.ID + 
       "<br />" + 
       "  Interface type: " + 
        conn.ProviderConnectionPoint.InterfaceType.ToString() + 
       "<br />" + 
       "  Control type: " + conn.ProviderConnectionPoint.ControlType.ToString() + 
       "<br />" + 
       "  Allows multiple connections: " + 
          conn.ProviderConnectionPoint.AllowsMultipleConnections.ToString() + 
       "<br />" + 
       "  Enabled: " + conn.ProviderConnectionPoint.GetEnabled(zip1).ToString() + 
       "<hr />" + 
       "<h3>Consumer Connection Point</h3>" + 
       "  Display name: " + conn.ConsumerConnectionPoint.DisplayName + 
       "<br />" + 
       "  ID: " + conn.ConsumerConnectionPoint.ID + 
       "<br />" + 
       "  Interface type: " + conn.ConsumerConnectionPoint.InterfaceType.ToString() + 
       "<br />" + 
       "  Control type: " + conn.ConsumerConnectionPoint.ControlType.ToString() + 
       "<br />" + 
       "  Allows multiple connections: " + 
          conn.ConsumerConnectionPoint.AllowsMultipleConnections.ToString() + 
       "<br />" + 
       "  Enabled: " + conn.ConsumerConnectionPoint.GetEnabled(zip1).ToString();
  }

  protected void Page_Load(object sender, EventArgs e)
  {
    lblConn.Text = String.Empty;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr" runat="server" >
        <StaticConnections>
          <asp:WebPartConnection ID="conn1"
            ConsumerConnectionPointID="ZipCodeConsumer"
            ConsumerID="weather1" 
            ProviderConnectionPointID="ZipCodeProvider" 
            ProviderID="zip1" />
        </StaticConnections>      
      </asp:WebPartManager>
      <uc1:displaymodemenucs id="menu1" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:ZipCodeWebPart ID="zip1" runat="server"
            Title="Zip Code Provider"  />
          <aspSample:WeatherWebPart ID="weather1" runat="server" 
            Title="Zip Code Consumer" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:ConnectionsZone ID="ConnectionsZone1" runat="server">
      </asp:ConnectionsZone>
      <asp:Button ID="Button1" runat="server" 
        Text="Dynamic Connection" 
        OnClick="Button1_Click" />      
      <br />
      <asp:Button ID="Button2" runat="server" 
        Text="Connection Point Details" 
        OnClick="Button2_Click" />
      <br />
      <asp:Label ID="lblConn" runat="server" />
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ register tagprefix="uc1" 
    tagname="DisplayModeMenuVB"
    src="~/displaymodemenuvb.ascx" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.VB.Controls" %>
    
<!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 System.EventArgs)

    Dim provPoint As ProviderConnectionPoint = _
      mgr.GetProviderConnectionPoints(zip1)("ZipCodeProvider")
    Dim connPoint As ConsumerConnectionPoint = _
      mgr.GetConsumerConnectionPoints(weather1)("ZipCodeConsumer")

    If mgr.CanConnectWebParts(zip1, provPoint, weather1, connPoint) Then
      mgr.ConnectWebParts(zip1, provPoint, weather1, connPoint)
    End If
    
  End Sub
  
  Protected Sub Button2_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    
    Dim conn As WebPartConnection = mgr.Connections(0)

    lblConn.Text = "<h2>Connection Point Details</h2>" & _
      "<h3>Provider Connection Point</h3>" & _
      "  Display name: " & conn.ProviderConnectionPoint.DisplayName & _
      "<br />" & _
      "  ID: " & conn.ProviderConnectionPoint.ID & _
      "<br />" & _
      "  Interface type: " & conn.ProviderConnectionPoint.InterfaceType.ToString() & _
      "<br />" & _
      "  Control type: " & conn.ProviderConnectionPoint.ControlType.ToString() & _
      "<br />" & _
      "  Allows multiple connections: " & _
        conn.ProviderConnectionPoint.AllowsMultipleConnections.ToString() & _
      "<br />" & _
      "  Enabled: " & conn.ProviderConnectionPoint.GetEnabled(zip1).ToString() & _
      "<hr />" & _
      "<h3>Consumer Connection Point</h3>" & _
      "  Display name: " & conn.ConsumerConnectionPoint.DisplayName & _
      "<br />" & _
      "  ID: " & conn.ConsumerConnectionPoint.ID & _
      "<br />" & _
      "  Interface type: " & conn.ConsumerConnectionPoint.InterfaceType.ToString() & _
      "<br />" & _
      "  Control type: " & conn.ConsumerConnectionPoint.ControlType.ToString() & _
      "<br />" & _
      "  Allows multiple connections: " & _
        conn.ConsumerConnectionPoint.AllowsMultipleConnections.ToString() & _
      "<br />" & _
      "  Enabled: " & conn.ConsumerConnectionPoint.GetEnabled(zip1).ToString()
          
  End Sub

  Protected Sub Page_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    lblConn.Text = String.Empty
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr" runat="server" >
        <StaticConnections>
          <asp:WebPartConnection ID="conn1"
            ConsumerConnectionPointID="ZipCodeConsumer"
            ConsumerID="weather1" 
            ProviderConnectionPointID="ZipCodeProvider" 
            ProviderID="zip1" />
        </StaticConnections>      
      </asp:WebPartManager>
      <uc1:displaymodemenuvb id="menu1" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:ZipCodeWebPart ID="zip1" runat="server"
            Title="Zip Code Provider"  />
          <aspSample:WeatherWebPart ID="weather1" runat="server" 
            Title="Zip Code Consumer" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:ConnectionsZone ID="ConnectionsZone1" runat="server">
      </asp:ConnectionsZone>
      <asp:Button ID="Button1" runat="server" 
        Text="Dynamic Connection" 
        OnClick="Button1_Click" />      
      <br />
      <asp:Button ID="Button2" runat="server" 
        Text="Connection Point Details" 
        OnClick="Button2_Click" />
      <br />
      <asp:Label ID="lblConn" runat="server" />
    </div>
    </form>
</body>
</html>

在浏览器中加载页面后,单击“ 连接点详细信息 ”按钮。 此时会显示有关在声明性连接中建立的提供程序和使用者连接点的信息。 接下来,使用 “显示模式 ”下拉控件将页面切换到连接模式。 在由标题栏中) 向下箭头表示的 邮政编码提供程序WebPart 控件 (谓词菜单上,单击连接谓词。 此时将显示连接 UI,该 UI 由页面中声明的 <asp:connectionszone> 控件自动创建。 这是通过 UI) 以及前面讨论的声明性和编程方法创建连接 (的另一种方法。 单击“ 断开连接 ”按钮终止现有的静态连接。 单击“ 创建与使用者的连接” 链接。 UI 现在显示一个下拉控件,其中列出了使用者连接点显示名称。 在下拉列表中选择连接点,然后单击“ 连接 ”以完成连接。 接下来,再次单击“ 断开连接 ”。 单击“ 动态连接 ”按钮以编程方式创建连接。 使用 “显示模式” 控件将页面返回到浏览模式。 再次单击“ 连接点详细信息 ”按钮,再次指示有关使用者连接点对象的详细信息。

该示例演示了如何通过三种方式建立连接并使用使用者连接点:在网页标记中声明的静态连接;在代码中创建的连接,该连接使用 ConsumerConnectionPoint 对象;以及用户通过连接 UI 创建的连接。

注解

在两个服务器控件之间的每个 Web 部件连接中,每个控件必须具有 () 关联的连接点对象,该对象使它能够连接到另一个控件并提供或使用数据,具体取决于控件是指定为连接的提供程序还是使用者。 一般情况下,对象 ConnectionPoint 包含控件如何连接到另一个控件及其可以共享的数据类型的详细信息。 对于在连接中充当使用者的控件,其连接点必须是 对象 ConsumerConnectionPoint 。 有关 Web 部件连接和连接点的详细信息,请阅读下面的“另请参阅”部分中列出的主题。

若要创建 ConsumerConnectionPoint 对象,需要执行几个步骤:

  1. 使使用者控件能够引用接口实例。 WebPart或其他服务器控件 (将添加到WebPartZoneBase区域的任何类型的服务器控件都可以使用,) 必须能够使用特定接口实例中的数据。 控件不需要实现 接口;只有提供程序必须实现它。 使用者可以使用提供程序提供的确切接口类型,或者,如果没有, WebPartTransformer 可以使用 对象将数据从提供程序的接口类型转换为使用者所理解的类型。 启用使用者的典型方法是声明一个私有字段,以包含对所需接口类型的引用。

  2. 标识回调方法。 必须将使用者中的方法标识为回调方法,才能与提供程序建立连接。 此方法检索提供程序实现的接口实例,并将其分配给 (例如,) 第一步中创建的私有字段。 用于在使用者中标识回调方法的 Web 部件方法是将类ConnectionConsumerAttribute) 定义的元数据属性添加到ConnectionConsumer接收接口实例的方法 (。 添加 属性时,唯一必需的参数是用于使用者连接点的显示名称。 还可以添加可选参数,例如 ID。

  3. 处理并输出接口实例中的数据。 根据需要对数据执行任何内部处理,然后使用者控件通常会将数据呈现到页面。 执行此操作的一种常见方法是重写 控件的 OnPreRender 方法。

    注意

    在同步请求期间,使用者应直接在事件期间或之后 PreRender 从提供程序请求数据。 在异步请求期间,如果在呈现期间任何时间点都没有调用提供程序的回调方法,则开发人员可以假定没有向使用者发送任何数据。

在控件被配置为充当使用者后,该控件可以参与连接 (假设提供程序控件也已配备并可用于) 。 若要在网页的标记中创建静态的声明性连接,开发人员可以使用 <asp:webpartconnection> 元素。 ConnectionConsumer如果使用者源代码中标识回调方法的属性指定连接点的 ID,则必须将该值分配给ConsumerConnectionPointID页面上 元素<asp:webpartconnection>中的 属性。 开发人员可能为使用者连接点指定 ID 的一个原因是,如果在使用者控件中定义了多个连接点。 如果未在使用者控件中为使用者连接点指定 ID,则也不必将值分配给 ConsumerConnectionPointID 页中的 属性,因为将使用从 DefaultID 字段获取的默认值创建连接。

若要在代码中创建连接,开发人员必须通过调用 GetConsumerConnectionPoints 方法并向其传递使用者控件的 ID 以及使用者控件中已定义ConsumerConnectionPoint对象的 ID 或索引来创建新的 ConsumerConnectionPoint 对象。 返回 ConsumerConnectionPoint 的对象以及对使用者控件的引用、对提供程序控件的引用以及相应的 ProviderConnectionPoint 对象都传递给 ConnectWebParts 方法以创建新 WebPartConnection 对象。

尽管开发人员可以使用使用者连接点作为以声明方式或编程方式建立连接的一部分,但用户还可以与使用者连接点进行交互,以通过用户界面 (UI) 建立连接。 如果开发人员在网页上声明 ConnectionsZone 控件,它将提供运行时 UI 供用户创建连接。 如果用户通过单击提供程序控件的连接谓词 (选择提供程序控件作为建立连接的起点,则还可以选择使用者;生成的连接) 没有区别,在 UI 中,他们将看到一个下拉列表控件,其中包含可用使用者连接点的显示名称 (或点(如果提供程序可以将数据发送到多个) )。 用户必须选择使用者连接点才能建立连接。

ConsumerConnectionPoint对象直接与特定的使用者控件关联,并将有关连接的详细信息存储在它从基ConnectionPoint类继承的属性中。 例如,在继承 InterfaceType 的属性中,使用者连接点保留它使用的接口类型。 如果连接中的提供程序和使用者都了解接口类型,则控件是兼容的,并且能够形成直接连接。 如果提供程序和使用者不能使用相同的接口类型,则它们不兼容,并且必须使用 WebPartTransformer 对象将提供程序连接点的 InterfaceType 属性转换为使用者可以使用的类型。 另一个重要的继承属性是 DisplayName 属性,它提供要在 UI 中显示的友好名称,供用户在创建连接时选择使用者连接点。 当开发人员将属性添加到 ConnectionConsumer 使用者控件中的回调方法时,显示名称是必需参数。 如上所述,继承 ID 的属性也很有用,因为在使用者具有多个连接点的情况下,它为使用者连接点提供唯一标识符。 使用者可以在其中定义多个 ConsumerConnectionPoint 对象,在这种情况下,当开发人员将 ConnectionConsumer 属性添加到方法时,他们应指定一个 ID 值来区分每个连接点。 另一个值得注意的继承属性是 AllowsMultipleConnections 属性,它指示使用者连接点是否可以同时连接到多个提供程序。 对于使用者连接点 (此属性值 false 默认为 ,而对于) 的提供程序连接点,此值默认 true 为 。

ConsumerConnectionPoint 将几个唯一方法添加到它从 ConnectionPoint 类继承的成员。 方法 SetObject 调用使用者自己定义的回调方法,以从提供程序检索接口实例。 方法 SupportsConnection 返回一个布尔值,该值指示连接点是否能够基于关联的使用者控件的当前状态建立连接。

构造函数

ConsumerConnectionPoint(MethodInfo, Type, Type, String, String, Boolean)

初始化 ConsumerConnectionPoint 类的新实例。

属性

AllowsMultipleConnections

获取一个指示连接点是否支持多个并发连接的值。

(继承自 ConnectionPoint)
ControlType

获取连接点与之关联的服务器控件的 Type

(继承自 ConnectionPoint)
DisplayName

获取一个字符串,该字符串用作在用户界面 (UI) 中表示连接点的友好显示名称。

(继承自 ConnectionPoint)
ID

获取包含连接点的标识符的字符串。

(继承自 ConnectionPoint)
InterfaceType

获取连接点所使用的接口的类型。

(继承自 ConnectionPoint)

方法

Equals(Object)

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

(继承自 Object)
GetEnabled(Control)

返回一个指示连接点是否可以参与连接的值。

(继承自 ConnectionPoint)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

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

调用使用者控件中的回调方法,并从提供者控件检索接口实例。

SupportsConnection(Control, ConnectionInterfaceCollection)

确定使用者连接点当前能否建立连接。

ToString()

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

(继承自 Object)

适用于

另请参阅