WebPartManager.CanConnectWebParts 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
檢查將參與連接的 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
可以參與連接。
傳回
布林值,指出 provider
和 consumer
是否可以連接。
範例
下列程式代碼範例示範如何使用這個方法。
程式代碼範例有四個部分:
使用者控制元件,可讓您變更網頁元件頁面上的顯示模式。
網頁,其中包含兩個可連接的自定義 WebPart 控件、專案
<asp:webpartmanager>
,以及一些使用 CanConnectWebParts 方法建立連接的事件處理程序代碼。包含兩個自定義 WebPart 控件和自定義介面的原始碼檔案。
說明範例在瀏覽器中的運作方式。
程式代碼範例的第一個部分是變更顯示模式的使用者控件。 您可以從類別概觀的 Example 區段取得使用者控制件的 WebPartManager 原始碼。 如需顯示模式和使用者控件運作方式的詳細資訊,請參閱逐步解說 :變更網頁元件頁面上的顯示模式。
網頁的宣告式標記包含 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,允許不相容的 provider
和 consumer
連接。
傳回
布林值,指出 provider
和 consumer
是否可以建立連接。
備註
當這兩個控件都有不相容的連接點類型時,會使用這個方法進行連接provider
consumer
,因此WebPartTransformer需要物件。 您可能想要使用這個方法來確認兩個控件可以在呼叫 ConnectWebParts 以建立程式設計連接之前連線。
此多載使用與方法多載相同的實 CanConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint) 作,唯一的例外狀況是此多載需要轉換器。