WebPartManager.CanConnectWebParts 方法

定義

檢查將參與連接的 WebPart 控制項,以判斷這些控制項是否可以連接。

多載

CanConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint)

當提供者和消費者控制項有相容的介面,並且不需要 WebPart 物件時,檢查將參與連接的 WebPartTransformer 控制項是否可以連接。

CanConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint, WebPartTransformer)

檢查將參與連接的 WebPart 控制項,以判斷它們是否可以連接,並且使用 WebPartTransformer 物件,來建立不相容消費者和提供者之間的連接。

備註

方法 CanConnectWebParts 可用來判斷是否可以連接兩 WebPart 個控制項。 方法通常會在呼叫 ConnectWebParts 方法之前做為條件式檢查。

方法 CanConnectWebParts 會檢查一些必須符合的準則,才能讓兩個控制項形成連接。 下列清單摘要說明建立連接的主要準則。 如果符合所有這些準則 (加上一些額外的內部條件) ,則方法會 true 傳回 ,這表示控制項可以連線:

  • 提供者和取用者控制項不能是 null ,而且它們必須包含在 屬性所 WebParts 參考的控制項集合中。

  • 提供者和取用者不能是相同的控制項。 換句話說, WebPart 控制項無法連接到本身。

  • 提供者 ConnectionPoint 和取用者) 的連接點 (物件不能是 null

  • 提供者和取用者無法關閉, (無法) 控制項的 IsClosed 屬性 true

  • 連接 ControlType 點控制項的 屬性必須符合取用者和提供者的控制項類型。

  • 連接點必須同時啟用, (其 GetEnabled 方法必須同時傳回 true) 。

  • 每個連接點都不能嘗試形成比本身 AllowsMultipleConnections 屬性中所指定的更多連接。

  • WebPartTransformer如果物件 (轉換程式) 連接不相容的控制項,它不能是 null 。 不過,如果控制項已經相容,轉換程式必須是 null

  • 如果使用) ,則必須在集合中 AvailableTransformers 參考轉換器 (。

  • 如果使用) 的轉換 (程式必須有與提供者和取用者相容的介面,才能轉換兩個控制項之間的資料。 取用者和提供者的次要介面也必須相容。

CanConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint)

當提供者和消費者控制項有相容的介面,並且不需要 WebPart 物件時,檢查將參與連接的 WebPartTransformer 控制項是否可以連接。

public:
 bool CanConnectWebParts(System::Web::UI::WebControls::WebParts::WebPart ^ provider, System::Web::UI::WebControls::WebParts::ProviderConnectionPoint ^ providerConnectionPoint, System::Web::UI::WebControls::WebParts::WebPart ^ consumer, System::Web::UI::WebControls::WebParts::ConsumerConnectionPoint ^ consumerConnectionPoint);
public bool CanConnectWebParts (System.Web.UI.WebControls.WebParts.WebPart provider, System.Web.UI.WebControls.WebParts.ProviderConnectionPoint providerConnectionPoint, System.Web.UI.WebControls.WebParts.WebPart consumer, System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint consumerConnectionPoint);
member this.CanConnectWebParts : System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.ProviderConnectionPoint * System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint -> bool
Public Function CanConnectWebParts (provider As WebPart, providerConnectionPoint As ProviderConnectionPoint, consumer As WebPart, consumerConnectionPoint As ConsumerConnectionPoint) As Boolean

參數

provider
WebPart

當兩個控制項連接時,提供資料給 consumer 的控制項。

providerConnectionPoint
ProviderConnectionPoint

provider 參與連接的 ConnectionPoint

consumer
WebPart

當兩個控制項連接時,從 provider 接收資料的控制項。

consumerConnectionPoint
ConsumerConnectionPoint

做為回呼方法的 ConnectionPoint,讓 consumer 可以參與連接。

傳回

Boolean

布林值,指出 providerconsumer 是否可以連接。

範例

下列程式碼範例示範如何使用此方法。

程式碼範例有四個部分:

  • 使用者控制項,可讓您變更Web 組件頁面上的顯示模式。

  • 網頁,其中包含兩個可連接的自訂 WebPart 控制項、專案 <asp:webpartmanager> ,以及一些使用 CanConnectWebParts 方法建立連接的事件處理常式代碼。

  • 包含兩個自訂 WebPart 控制項和自訂介面的原始程式碼檔案。

  • 說明範例如何在瀏覽器中運作。

程式碼範例的第一個部分是用來變更顯示模式的使用者控制項。 您可以從類別概觀的 WebPartManager 範例區段取得使用者控制項的原始程式碼。 如需顯示模式和使用者控制項運作方式的詳細資訊,請參閱逐步解說:變更Web 組件頁面上的顯示模式

網頁的宣告式標記包含 Register 使用者控制項和自訂控制項的指示詞。 有專案 <asp:webpartmanager><asp:webpartzone> 要包含自訂控制項的專案,以及專案 <asp:connectionszone> 。 請注意,在 方法中 Page_Load ,程式碼會檢查是否可以建立連接,如果是,則會定義提供者、取用者及其各自的連接點,然後將新的連接加入至 屬性所 StaticConnections 參考的靜態連接集。

<%@ Page Language="C#" %>
<%@ register TagPrefix="uc1" 
  TagName="DisplayModeMenuCS" 
  Src="DisplayModeMenuCS.ascx" %>
<%@ register tagprefix="aspSample" 
  Namespace="Samples.AspNet.CS.Controls" 
  Assembly="ConnectionSampleCS"%>

<!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 Page_Load(object sender, EventArgs e)
  {
    
    // Define provider, consumer, and connection points.
    WebPart provider = mgr.WebParts["zip1"];
    ProviderConnectionPoint provConnPoint =
      mgr.GetProviderConnectionPoints(provider)["ZipCodeProvider"];
    WebPart consumer = mgr.WebParts["weather1"];
    ConsumerConnectionPoint consConnPoint =
      mgr.GetConsumerConnectionPoints(consumer)["ZipCodeConsumer"];
    
    // Check whether the connection already exists.
    if (mgr.CanConnectWebParts(provider, provConnPoint,
      consumer, consConnPoint))
    {
      // Create a new static connection.
      WebPartConnection conn = new WebPartConnection();
      conn.ID = "staticConn1";
      conn.ConsumerID = "weather1";
      conn.ConsumerConnectionPointID = "ZipCodeConsumer";
      conn.ProviderID = "zip1";
      conn.ProviderConnectionPointID = "ZipCodeProvider";
      mgr.StaticConnections.Add(conn);
    }
 }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <!-- Reference the WebPartManager control. -->
      <asp:WebPartManager ID="mgr" runat="server" />   
    <div>
      <uc1:DisplayModeMenuCS ID="displaymode1" 
        runat="server" />
      <!-- Reference consumer and provider controls 
           in a zone. -->
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:ZipCodeWebPart ID="zip1" 
            runat="server" 
            Title="Zip Code Control"/>
          <aspSample:WeatherWebPart ID="weather1" 
            runat="server" 
            Title="Weather Control" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <!-- Add a ConnectionsZone so users can connect 
           controls. -->
      <asp:ConnectionsZone ID="ConnectionsZone1" 
        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" 
  Assembly="ConnectionSampleVB"%>

<!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 Page_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    
    ' Define provider, consumer, and connection points.
    Dim provider As WebPart = mgr.WebParts("zip1")
    Dim provConnPoint As ProviderConnectionPoint = _
      mgr.GetProviderConnectionPoints(provider)("ZipCodeProvider")
    Dim consumer As WebPart = mgr.WebParts("weather1")
    Dim consConnPoint As ConsumerConnectionPoint = _
      mgr.GetConsumerConnectionPoints(consumer)("ZipCodeConsumer")
    
    ' Check whether the connection already exists.
    If mgr.CanConnectWebParts(provider, provConnPoint, _
      consumer, consConnPoint) Then
      ' Create a new static connection.
      Dim conn As New WebPartConnection()
      conn.ID = "staticConn1"
      conn.ConsumerID = "weather1"
      conn.ConsumerConnectionPointID = "ZipCodeConsumer"
      conn.ProviderID = "zip1"
      conn.ProviderConnectionPointID = "ZipCodeProvider"
      mgr.StaticConnections.Add(conn)
    End If
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
      <!-- Reference the WebPartManager control. -->
      <asp:WebPartManager ID="mgr" runat="server" />   
    <div>
      <uc1:DisplayModeMenuVB ID="displaymode1" 
        runat="server" />
      <!-- Reference consumer and provider controls 
           in a zone. -->
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:ZipCodeWebPart ID="zip1" 
            runat="server" 
            Title="Zip Code Control"/>
          <aspSample:WeatherWebPart ID="weather1" 
            runat="server" 
            Title="Weather Control" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <!-- Add a ConnectionsZone so users can connect 
           controls. -->
      <asp:ConnectionsZone ID="ConnectionsZone1" 
        runat="server" />
    </div>
    </form>
</body>
</html>

範例的第三個部分是控制項的原始程式碼。 它包含介面和兩個自訂 WebPart 控制項,一個做為提供者,另一個做為取用者。 因為它們有相容的連接點 (兩者都辨識 IZipCode 介面) ,因此不需要轉換程式才能進行連線。 若要執行程式碼範例,您必須編譯此原始程式碼。 您可以明確地編譯它,並將產生的元件放在網站的 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", "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", "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", "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", "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

在瀏覽器中載入網頁之後,按一下 [顯示模式] 下拉式清單控制項,然後選取 [連線 切換頁面以連線模式。 連線模式會 <asp:connectionszone> 使用 元素,讓您建立控制項之間的連線。 在連線模式中,按一下 [郵遞區號] 控制項標題列中的向下箭號以啟動其動詞功能表,然後按一下 [連線]。 出現連接使用者介面 (UI) 之後,請注意方法中包含的 Page_Load 程式碼已經建立連線。

備註

這個方法可用來連接 provider ,而且 consumer 這兩個控制項都有相容的連接點類型時,因此 WebPartTransformer 不需要 物件。 您可能想要使用此方法來確認兩個控制項可以連線,再呼叫 ConnectWebParts 以建立程式設計連線。

此多載使用與方法多載相同的實 CanConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint, WebPartTransformer) 作,唯一的例外狀況是此多載不需要轉換器。

另請參閱

適用於

CanConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint, WebPartTransformer)

檢查將參與連接的 WebPart 控制項,以判斷它們是否可以連接,並且使用 WebPartTransformer 物件,來建立不相容消費者和提供者之間的連接。

public:
 virtual bool CanConnectWebParts(System::Web::UI::WebControls::WebParts::WebPart ^ provider, System::Web::UI::WebControls::WebParts::ProviderConnectionPoint ^ providerConnectionPoint, System::Web::UI::WebControls::WebParts::WebPart ^ consumer, System::Web::UI::WebControls::WebParts::ConsumerConnectionPoint ^ consumerConnectionPoint, System::Web::UI::WebControls::WebParts::WebPartTransformer ^ transformer);
public virtual bool CanConnectWebParts (System.Web.UI.WebControls.WebParts.WebPart provider, System.Web.UI.WebControls.WebParts.ProviderConnectionPoint providerConnectionPoint, System.Web.UI.WebControls.WebParts.WebPart consumer, System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint consumerConnectionPoint, System.Web.UI.WebControls.WebParts.WebPartTransformer transformer);
abstract member CanConnectWebParts : System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.ProviderConnectionPoint * System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint * System.Web.UI.WebControls.WebParts.WebPartTransformer -> bool
override this.CanConnectWebParts : System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.ProviderConnectionPoint * System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint * System.Web.UI.WebControls.WebParts.WebPartTransformer -> bool
Public Overridable Function CanConnectWebParts (provider As WebPart, providerConnectionPoint As ProviderConnectionPoint, consumer As WebPart, consumerConnectionPoint As ConsumerConnectionPoint, transformer As WebPartTransformer) As Boolean

參數

provider
WebPart

當兩個控制項連接時,提供資料給 consumer 的控制項。

providerConnectionPoint
ProviderConnectionPoint

做為回呼方法的 ConnectionPoint,讓 provider 可以參與連接。

consumer
WebPart

當兩個控制項連接時,從 provider 接收資料的控制項。

consumerConnectionPoint
ConsumerConnectionPoint

做為回呼方法的 ConnectionPoint,讓 consumer 可以參與連接。

transformer
WebPartTransformer

WebPartTransformer,允許不相容的 providerconsumer 連接。

傳回

Boolean

布林值,指出 providerconsumer 是否可以建立連接。

備註

這個方法可用來連接 provider ,而且 consumer 這兩個控制項都有不相容的連接點類型時,因此 WebPartTransformer 需要 物件。 您可能想要使用此方法來確認兩個控制項可以連線,再呼叫 ConnectWebParts 以建立程式設計連線。

此多載會使用與 CanConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint) 方法多載相同的實作,唯一的例外狀況是此多載需要轉換器。

適用於