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简单接口。 还有一个名为 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

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

该页提供了三种在自定义控件之间建立连接的方法。 第一种方法是声明性的。 在页面的标记中,声明了 一个 <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 在控件之间创建连接。 这是创建连接的第二种方法。 请注意,一旦建立连接,在提供程序控件中输入的最后一个字符串 (,由于控件断开连接) 突然出现在使用者中,因为连接已重新创建,因此无法显示。 单击“ 断开连接 ”按钮以结束刚刚创建的当前连接。 将页面返回到浏览模式。 在提供程序中输入一些新文本,以表明文本未更新,并且控件再次断开连接。

返回用于连接显示模式的页面。 单击“ 连接 Web 部件控件 ”按钮,而不是单击连接谓词,该按钮演示了形成连接的第三种方法。 此方法通过一个简单的步骤以编程方式连接控件,而无需使用该 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 引用 对象的 ID ConsumerConnectionPoint 。 所有这些与使用者相关的连接属性都必须分配有一个值才能创建连接。

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() 方法,并返回连接对象的类型的简称。

适用于

另请参阅