WebPartManager.ConnectWebParts 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立兩個控制點(或其他能建立連接的伺服器控制點)之間的連接 WebPart ,這些控制點位於同一 WebPartZoneBase 區域內。
多載
| 名稱 | Description |
|---|---|
| ConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint) |
僅利用對控制項及其指定WebPart物件的參考,建立兩個GenericWebPart或ConnectionPoint控制項之間的連結。 |
| ConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint, WebPartTransformer) |
利用對控制項、其指定WebPart物件的參考,以及一個GenericWebPart物件,建立兩個ConnectionPoint或WebPartTransformer控制項之間的連結。 |
備註
此 ConnectWebParts 方法在任意 WebPart 兩個可連接的控制項之間建立連結。 在呼叫此方法建立連線前,你也可以先以條件檢查方式呼叫該 CanConnectWebParts 方法,以確保控制項符合建立連接的要求。
Note
也可以在兩個非 WebPart 控制項的伺服器控制項之間建立連線。 一般來說,這兩個控制項必須是自訂的伺服器控制項(例如繼承自 WebControl 或現有的 ASP.NET 伺服器控制項),這樣你才能新增所需的成員。 控制措施也必須符合以下規定的要求。
任何控制控制間的連接情境都必須符合以下條件才能連接:
每個控制點都位於一個 WebPartZoneBase 區域內(不一定是同一個區域)。
依照 Web Parts 控制集的實作,連線中的提供者控制以公開方法形式實作介面,作為提供者的回調,並在方法上有一個
ConnectionProvider元資料屬性以識別其為提供者連接點。 由於擷取提供者連接點的方法 GetProviderConnectionPoints 為虛擬,導出 WebPartManager 控制不一定必須使用相同的元資料屬性。在 Web Parts 控制集中實作時,連線中的消費者控制項也有特殊方法,能取得提供者回調方法中暴露的介面參考,且消費者在該方法上有一個
ConnectionConsumer元資料屬性,以識別其為消費者連接點。 由於 GetConsumerConnectionPoints 取回消費者連接點的方法為虛擬,衍生 WebPartManager 控制不一定必須使用相同的元資料屬性。回撥方法必須要麼相容,即消費者可以使用提供者回撥方法中提供的介面類型(即消費者與提供者可直接共享資料),要麼開發者必須使用 WebPartTransformer 物件將提供者的資料轉換成消費者可使用的格式。
Important
當你不需要變壓器時,可以使用 ConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint) 方法過載。 當你需要變壓器時,使用 ConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint, WebPartTransformer) 方法過載。
ConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint)
僅利用對控制項及其指定WebPart物件的參考,建立兩個GenericWebPart或ConnectionPoint控制項之間的連結。
public:
System::Web::UI::WebControls::WebParts::WebPartConnection ^ ConnectWebParts(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 System.Web.UI.WebControls.WebParts.WebPartConnection ConnectWebParts(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.ConnectWebParts : 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.WebPartConnection
Public Function ConnectWebParts (provider As WebPart, providerConnectionPoint As ProviderConnectionPoint, consumer As WebPart, consumerConnectionPoint As ConsumerConnectionPoint) As WebPartConnection
參數
- providerConnectionPoint
- ProviderConnectionPoint
一種作為連線回撥方法的方法。 在 Web Parts 控制集中實作時,這是一個以provider元資料屬性標記的公開方法ConnectionProvider。
- consumerConnectionPoint
- ConsumerConnectionPoint
一種連接 以 providerConnectionPoint 接收連線資料的方法。 在 Web Parts 控制集中實作時,這是一個以consumer元資料屬性標記的公開方法ConnectionConsumer。
傳回
WebPartConnection其中包含關於服務提供者與消費者的各種資訊,以便建立連線。
例外狀況
WebPartManager控制項的動態集合是唯讀的。
範例
以下程式碼範例示範如何利用此方法在程式上建立連線。 關於執行範例所需的完整程式碼,請參閱類別總覽的 WebPartManager 範例部分。 從這個例子來看,你需要使用者控制項的原始碼,讓你能在頁面上更改顯示模式,以及兩個自訂 WebPart 控制項的原始碼。
以下是承載兩個控制項的網頁程式碼。 頁面頂部使用 Register 指令來宣告使用者控制項與自訂控制項。 自訂控制項會在元素 <asp:webpartzone> 中以宣告式方式引用。 處理 Button1_Click 該方法的程式碼透過使用該 ConnectWebParts 方法建立控制項之間的連結。
<%@ 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 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">
</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"
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 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">
</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>
在瀏覽器載入頁面後,點擊 「Connect WebPart Controls 」按鈕以建立連線。 接著你可以在文字框輸入一些資料,並點擊 輸入五位數郵遞區號 按鈕,以顯示兩個控制項已連接,且第一個控制項輸入的資料會更新到第二個控制項。
備註
當控制節點足夠相容,能無需使用 WebPartTransformer 物件即可連接時,會用過載方式連接控制節點。 當呼叫這個方法的超載時,它會直接將呼叫傳給另一個超載版本的方法,並傳遞 null 需要物件的 WebPartTransformer 參數。
當你嘗試以程式方式連接兩個控制項時,可以用 CanConnectWebParts 條件檢查方法判斷控制項是否能直接連接。
另請參閱
- GetConsumerConnectionPoints(WebPart)
- GetProviderConnectionPoints(WebPart)
- Connections
- ASP.NET 網頁零件控制
適用於
ConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint, WebPartTransformer)
利用對控制項、其指定WebPart物件的參考,以及一個GenericWebPart物件,建立兩個ConnectionPoint或WebPartTransformer控制項之間的連結。
public:
virtual System::Web::UI::WebControls::WebParts::WebPartConnection ^ ConnectWebParts(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 System.Web.UI.WebControls.WebParts.WebPartConnection ConnectWebParts(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 ConnectWebParts : 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 -> System.Web.UI.WebControls.WebParts.WebPartConnection
override this.ConnectWebParts : 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 -> System.Web.UI.WebControls.WebParts.WebPartConnection
Public Overridable Function ConnectWebParts (provider As WebPart, providerConnectionPoint As ProviderConnectionPoint, consumer As WebPart, consumerConnectionPoint As ConsumerConnectionPoint, transformer As WebPartTransformer) As WebPartConnection
參數
- providerConnectionPoint
- ProviderConnectionPoint
其中的公開方法 provider 會標記為中 ConnectionProvider 繼資料屬性,並作為連線的回調方法。
- consumerConnectionPoint
- ConsumerConnectionPoint
其中的公開方法 consumer 標記為中 ConnectionConsumer 繼資料屬性,並連接以 providerConnectionPoint 接收該連線的資料。
- transformer
- WebPartTransformer
WebPartTransformer A透過將資料provider轉換成可consumer處理的格式,使兩個控制項之間能夠建立連結。
傳回
WebPartConnection包含連接所需的提供者、消費者及變壓器的資訊。
例外狀況
連線已在 中啟用 PreRender。
備註
當控制點不相容時,會用這種過載方式連接控制點。 不相容發生在 consumer 實作介面與 provider 其連接點不同的介面時。 變壓器將資料轉換成可由 理解 consumer的型態。
另請參閱
- GetConsumerConnectionPoints(WebPart)
- GetProviderConnectionPoints(WebPart)
- Connections
- WebPartTransformer
- ASP.NET 網頁零件控制