WebPartConnection 类

定义

提供一个对象,该对象可使两个 WebPart 控件形成连接。 此类不能被继承。

public ref class WebPartConnection sealed
[System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
public sealed class WebPartConnection
[<System.ComponentModel.TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))>]
type WebPartConnection = class
Public NotInheritable Class WebPartConnection
继承
WebPartConnection
属性

示例

下面的代码示例演示如何在两 WebPart 个控件之间创建简单的连接。 本示例演示了三种方法来形成连接:以声明方式将连接标记放在网页的标记中;通过编程方式在代码中创建连接;并通过 UI 将控件放在 ConnectionsZone 页面上,使用户能够建立连接。

代码示例包含四个部分:

  • 允许用户更改页面上的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 的简单接口。 还有一个名为 WebPart 实现接口并充当提供程序控件的类 ZipCodeWebPart 。 其 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

代码示例的第三部分是网页。 靠近顶部是 Register 用户控件和自定义 WebPart 控件的指令。 由于该示例假定控件的动态编译,因此控件的源代码应位于App_Code子文件夹中; Register 页中的标记仅引用任意标记前缀和控件的命名空间。 (提供程序和使用者) 的自定义WebPart控件在网页元素<zonetemplate><asp:webpartzone>声明。

该页提供了三种在自定义控件之间形成连接的方法。 第一种方法是声明性的。 在页面的标记中,声明一个 <StaticConnections> 元素,并在其中声明一个 <asp:WebPartConnections> 元素,其中包含指定为属性的连接的各种使用者和提供程序详细信息。 这是通过直接在网页中声明连接(特别是在元素内 <asp:WebPartManager> )创建连接的一种方法。 由于此静态连接,页面首次加载时会立即创建两个自定义控件之间的连接。

用于在控件之间形成连接的第二种方法由 <asp:connectionszone> 页面中的元素提供。 如果用户在运行时将页面切换到连接显示模式,并在其中一个自定义控件上单击连接谓词,则 <asp:connectionszone> 元素会自动呈现用于创建连接的 UI。

该页还演示了创建连接的第三种方法,即以编程方式执行此操作。 在 Button1_Click 方法中,代码为提供程序控件创建一个 ProviderConnectionPoint 对象,并通过调用 GetProviderConnectionPoints 该方法检索其连接点详细信息。 它为使用者控件执行类似的任务,调用该方法 GetConsumerConnectionPoints 。 最后,它通过对控件调用ConnectWebParts方法WebPartManager来创建新WebPartConnection对象。

<%@ 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"];
    WebPartConnection conn1 = mgr.ConnectWebParts(zip1, provPoint,
      weather1, connPoint);
  }

  protected void mgr_DisplayModeChanged(object sender, 
    WebPartDisplayModeEventArgs e)
  {
    if (mgr.DisplayMode == WebPartManager.ConnectDisplayMode)
      Button1.Visible = true;
    else
      Button1.Visible = false;
  }
</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" 
    OnDisplayModeChanged="mgr_DisplayModeChanged">
        <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="Connect WebPart Controls" 
        OnClick="Button1_Click" 
    Visible="false" />
    </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")
    mgr.ConnectWebParts(zip1, provPoint, weather1, connPoint)

  End Sub

  Protected Sub mgr_DisplayModeChanged (ByVal sender as Object, _
    ByVal e as WebPartDisplayModeEventArgs)

    If mgr.DisplayMode Is WebPartManager.ConnectDisplayMode Then
    Button1.Visible = True
    Else
    Button1.Visible = False
    End If

  End Sub
</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" 
    OnDisplayModeChanged="mgr_DisplayModeChanged">
        <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="Connect WebPart Controls" 
        OnClick="Button1_Click" 
    Visible="false" />
    </div>
    </form>
</body>
</html>

在浏览器中加载网页后,第一个连接已存在,因为它在元素中 <StaticConnections> 声明。 在 邮政编码提供程序 控件中输入一些文本,它将显示在使用者控件中。 接下来,断开两个控件的连接。 使用 “显示模式” 下拉列表控件,更改页面以连接显示模式。 单击标题栏中) 每个控件的向下箭头表示的谓词菜单 (,WebPart并注意到每个控件都有 一个连接 选项。 这是一个连接谓词,仅在页面处于连接模式时,该谓词才会显示在谓词菜单中。 单击其中一个控件上的连接谓词,并显示控件提供 ConnectionsZone 的连接 UI。 单击“ 断开连接 ”按钮结束控件之间的静态连接。 使用 显示模式 控件返回页面以浏览模式。 尝试再次在提供程序中输入一些新文本,并请注意,由于控件断开连接,因此文本无法在使用者控件中更新。

接下来,使用与上面的方法相同的方法将页面切换为连接显示模式。 单击其中一个控件上的连接谓词。 单击“ 创建连接” 链接,并使用控件提供的 ConnectionsZone UI 在控件之间创建连接。 这是创建连接的第二种方法。 请注意,一旦形成连接,在提供程序控件中输入的最后一个字符串 (因控件断开连接而无法显示,) 突然出现在使用者中,因为连接已重新创建。 单击“ 断开连接 ”按钮以结束刚刚创建的当前连接。 返回页面以浏览模式。 在提供程序中输入一些新文本,以演示文本未更新,并且控件再次断开连接。

返回页面以连接显示模式。 单击 连接 WebPart 控件 按钮,而不是单击连接谓词,这说明了构成连接的第三种方法。 此方法在一个简单的步骤中以编程方式连接控件,而无需使用该 ConnectionsZone 控件。 请注意,创建连接时,在提供程序中输入的最后一个字符串突然出现在使用者控件中。

注解

在Web 部件控件集中,连接是两WebPart个 (或其他服务器或用户) 控件之间的链接或关联,使它们能够共享数据。 共享数据的功能允许通过超出独立控件提供的功能的方式使用连接的控件。 例如,如果一个控件提供邮政编码数据,另一个控件可以读取该数据,并根据邮政编码提供本地天气信息,则两个控件的连接功能为用户提供更多价值。 为了扩展此示例,可以创建其他控件,这些控件也可以基于邮政编码显示信息,例如包含指向本地新闻的链接的控件,所有这些控件都可以使用邮政编码数据与提供邮政编码的单个控件共享数据。 Web 部件应用程序的最终用户可以直接从 Web 浏览器创建和管理所有此类兼容控件之间的连接、使用控件提供ConnectionsZone的标准连接用户界面 (UI) ,或使用开发人员提供的自定义 UI。

此类 WebPartConnection 概述是创建连接的基本详细信息的一般语句。 有关创建连接所涉及的特定组件和要求的详细信息,请参阅Web 部件连接概述,或参阅以下讨论中提到的参考类和代码示例。 Web 部件连接有几个基本方面:

  • 两个 WebPart 控件。 每个Web 部件连接由两个控件组成。 控件可以同时参与多个连接,但每个连接都包含两个控件。 控件可以直接从WebPart基类派生,也可以是其他服务器控件,包括 ASP.NET 控件、自定义服务器控件和用户控件。 如果不派生自类的 WebPart 控件(如果放置在 WebPartZoneBase 区域中),则会自动在运行时与对象一起 GenericWebPart 包装,这样它们就可以从 WebPart 类继承,并充当运行时 WebPart 控件。

  • 驻留在 WebPartZoneBase 区域中的控件。 WebPart控件和任何其他类型的服务器控件都必须驻留在一个WebPartZoneBase区域中,才能参与Web 部件连接 (和其他大多数Web 部件功能) 。

  • 使用者和提供程序。 在每个Web 部件连接中,都有两个控件:数据提供程序和数据使用者。 提供程序通过指定的回调方法向使用者提供数据,该方法以接口的形式返回数据。 (有关如何创建和指定回调方法的示例,请参阅本主题的示例部分。) 此回调方法称为提供程序连接点。 此连接点的详细信息 (其“友好”名称、ID 和返回接口的类型) 包含在与提供程序控件关联的对象中 ProviderConnectionPoint 。 使用者通过可接受接口实例的指定方法接收数据。 此方法称为使用者连接点,连接点的详细信息 (名称、ID 和接口类型) 包含在与使用者控件关联的对象中 ConsumerConnectionPoint

  • 兼容的控件或有效的转换器。 若要使连接正常工作,使用者和提供程序必须兼容 (这意味着其指定的连接点方法可以使用同一类型的接口) ,或者必须有一个 WebPartTransformer 对象能够将提供程序提供的类型转换为使用者理解的类型。

  • WebPartConnection 对象。 若要建立连接,必须存在包含对提供程序和使用者控件的引用的类的实例 WebPartConnection 及其连接点的详细信息。 如果提供程序和使用者不兼容,而是使用 WebPartTransformer 对象进行连接,则连接引用转换器。

  • 建立连接的方法。 使用连接点方法正确设计并放置在区域中 WebPartConnection 且对象可用后,最后一个基本步骤是启动连接。 发生这种情况的一种方法是让用户通过 UI 创建连接。 如果在页面上放置元素 <asp:connectionszone> ,并且连接的其他必需组件已到位,在运行时,用户可以将页面切换到连接显示模式,请在提供程序或使用者的谓词菜单上单击连接谓词,并且将显示基于 ConnectionsZone 控件) 的连接 UI (。 通过此 UI,用户可以启动连接。 启动连接的另一种方法是以编程方式执行此操作。 无论通过 UI 还是以编程方式,启动连接的基础方法都是相同的。 如果对控件使用转换器) WebPartManager,则应用程序调用ConnectWebParts该方法 (或ConnectWebParts方法,并将其传递给提供程序、使用者及其各自的连接点对象,并且该方法返回一个WebPartConnection对象。

WebPartConnection 类定义一个对象,该对象封装两 WebPart 个控件之间的连接的基本详细信息。 该类几乎完全包含与特定连接的详细信息相关的属性。 多个属性涉及连接中的使用者控件。 该 Consumer 属性引用使用者控件本身,属性 ConsumerID 引用使用者的 ID。 该 ConsumerConnectionPoint 对象包含使用者的连接点的详细信息,由使用者 ConsumerConnectionPoint 的属性引用。 该 ConsumerConnectionPointID 属性引用对象的 ConsumerConnectionPoint ID。 所有这些与使用者相关的连接属性必须为其分配一个值才能创建连接。

WebPartConnection 类还具有多个与连接中的提供程序控件相关的属性,这些属性对应于使用者的属性。 属性 Provider 引用提供程序控件本身,而 ProviderID 属性引用其 ID。 该 ProviderConnectionPoint 属性引用对象 ProviderConnectionPoint ,属性 ProviderConnectionPointID 引用提供程序连接点的 ID。

多个属性涉及连接的状态。 该 IsActive 属性指示连接是活动 (当前交换数据) 还是非活动 (仍已连接,但未主动共享数据) 。 该 IsShared 属性指示连接是可供页面) 或特定于用户的所有用户使用的共享 (,并且 IsStatic 该属性指示控件是静态 (声明在页面标记中,从而以编程方式创建永久) 还是动态 (,这意味着它可以删除) 。

构造函数

WebPartConnection()

初始化 WebPartConnection 类的新实例。

属性

Consumer

获取一个作为连接中的使用者控件的 WebPart 对象。

ConsumerConnectionPoint

获取一个对象,该对象作为连接中充当使用者的控件的连接点。

ConsumerConnectionPointID

获取或设置连接上的属性值,该值引用作为该连接的使用者连接点的对象的 ID。

ConsumerID

获取或设置连接上的属性值,该值引用作为该连接的使用者的 WebPart 控件的 ID。

ID

获取或设置 WebPartConnection 对象的 ID。

IsActive

获取一个值,该值指示 WebPartConnection 对象当前是否已经建立并且能够在其提供者和使用者控件之间交换数据。

IsShared

获取一个值,该值指示 WebPartConnection 对象是对所有用户可见还是仅对当前用户可见。

IsStatic

获取一个值,该值指示 WebPartConnection 对象是在网页的标记中声明的,还是以编程方式创建的。

Provider

获取作为 Web 部件连接中的提供者的 WebPart 控件。

ProviderConnectionPoint

获取一个对象,该对象作为充当连接提供者的 WebPart 控件的连接点。

ProviderConnectionPointID

获取或设置连接上的属性值,该值引用作为该连接的提供者连接点的对象的 ID。

ProviderID

获取或设置连接上的属性值,该值引用作为该连接的提供者的 WebPart 控件的 ID。

Transformer

获取一个 WebPartTransformer 对象,用于在 Web 部件连接中两个不兼容的连接点之间转换数据。

Transformers

获取由 Web 部件控件集内部使用的 WebPartTransformer 对象的集合。

方法

Equals(Object)

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

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

重写继承的 ToString() 方法,并返回连接对象的类型的简称。

适用于

另请参阅