ConnectionPoint 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
做為定義連接點物件的基底類別,這些物件可讓 Web 組件連接中的消費者控制項和提供者控制項共用資料。
public ref class ConnectionPoint abstract
public abstract class ConnectionPoint
type ConnectionPoint = class
Public MustInherit Class ConnectionPoint
- 繼承
-
ConnectionPoint
- 衍生
範例
下列程式代碼範例示範如何建立包含必要 ConnectionPoint 對象的網頁元件連線。 因為類別 ConnectionPoint 是抽象基類,所以其子類別的實例--ProviderConnectionPoint 和 ConsumerConnectionPoint--是用來形成連接的實際物件。
此範例有四個部分:
使用者控制元件,可讓您變更頁面上的網頁元件顯示模式。
介面和兩 WebPart 個控件的原始程式碼,做為連線的提供者和取用者。
用來裝載所有控制項並執行程式碼範例的網頁。
如何執行範例頁面的說明。
此程式代碼範例的第一個部分是使用者控制項,可讓使用者變更網頁上的顯示模式。 將下列原始程式碼儲存至 .ascx 檔案,為它指定指派給 Src
這個使用者控件之 Register
指示詞的 屬性的檔名,這靠近主控網頁頂端。 如需此控件中原始碼顯示模式和描述的詳細資訊,請參閱逐步解說 :變更網頁元件頁面上的顯示模式。
<%@ 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
簡單介面。 另外還有一ZipCodeWebPart
個名為 WebPart 的類別會實作 介面,並做為提供者控件。 其 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
註冊構成連線的自定義控件,以及可讓使用者變更頁面上顯示模式的使用者控件。 連接本身是以宣告方式在頁面上的 元素內 <staticconnections>
建立。 您也可以以程序設計方式建立連線;用於執行該作業的程式代碼包含在方法中 Button1_Click
。 無論是以宣告方式或以程序設計方式建立連接,都必須同時為提供者和取用者指定連接點。 方法 Button2_Click
會 ConnectionPoint 同時存取提供者和取用者的 物件,並將其中一些屬性值寫入頁面中的標籤。
<%@ 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"];
if(mgr.CanConnectWebParts(zip1, provPoint, weather1, connPoint))
mgr.ConnectWebParts(zip1, provPoint, weather1, connPoint);
}
protected void Button2_Click(object sender, EventArgs e)
{
WebPartConnection conn = mgr.Connections[0];
lblConn.Text = "<h2>Connection Point Details</h2>" +
"<h3>Provider Connection Point</h3>" +
" Display name: " + conn.ProviderConnectionPoint.DisplayName +
"<br />" +
" ID: " + conn.ProviderConnectionPoint.ID +
"<br />" +
" Interface type: " +
conn.ProviderConnectionPoint.InterfaceType.ToString() +
"<br />" +
" Control type: " + conn.ProviderConnectionPoint.ControlType.ToString() +
"<br />" +
" Allows multiple connections: " +
conn.ProviderConnectionPoint.AllowsMultipleConnections.ToString() +
"<br />" +
" Enabled: " + conn.ProviderConnectionPoint.GetEnabled(zip1).ToString() +
"<hr />" +
"<h3>Consumer Connection Point</h3>" +
" Display name: " + conn.ConsumerConnectionPoint.DisplayName +
"<br />" +
" ID: " + conn.ConsumerConnectionPoint.ID +
"<br />" +
" Interface type: " + conn.ConsumerConnectionPoint.InterfaceType.ToString() +
"<br />" +
" Control type: " + conn.ConsumerConnectionPoint.ControlType.ToString() +
"<br />" +
" Allows multiple connections: " +
conn.ConsumerConnectionPoint.AllowsMultipleConnections.ToString() +
"<br />" +
" Enabled: " + conn.ConsumerConnectionPoint.GetEnabled(zip1).ToString();
}
protected void Page_Load(object sender, EventArgs e)
{
lblConn.Text = String.Empty;
}
</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" >
<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="Dynamic Connection"
OnClick="Button1_Click" />
<br />
<asp:Button ID="Button2" runat="server"
Text="Connection Point Details"
OnClick="Button2_Click" />
<br />
<asp:Label ID="lblConn" 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" %>
<!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")
If mgr.CanConnectWebParts(zip1, provPoint, weather1, connPoint) Then
mgr.ConnectWebParts(zip1, provPoint, weather1, connPoint)
End If
End Sub
Protected Sub Button2_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim conn As WebPartConnection = mgr.Connections(0)
lblConn.Text = "<h2>Connection Point Details</h2>" & _
"<h3>Provider Connection Point</h3>" & _
" Display name: " & conn.ProviderConnectionPoint.DisplayName & _
"<br />" & _
" ID: " & conn.ProviderConnectionPoint.ID & _
"<br />" & _
" Interface type: " & conn.ProviderConnectionPoint.InterfaceType.ToString() & _
"<br />" & _
" Control type: " & conn.ProviderConnectionPoint.ControlType.ToString() & _
"<br />" & _
" Allows multiple connections: " & _
conn.ProviderConnectionPoint.AllowsMultipleConnections.ToString() & _
"<br />" & _
" Enabled: " & conn.ProviderConnectionPoint.GetEnabled(zip1).ToString() & _
"<hr />" & _
"<h3>Consumer Connection Point</h3>" & _
" Display name: " & conn.ConsumerConnectionPoint.DisplayName & _
"<br />" & _
" ID: " & conn.ConsumerConnectionPoint.ID & _
"<br />" & _
" Interface type: " & conn.ConsumerConnectionPoint.InterfaceType.ToString() & _
"<br />" & _
" Control type: " & conn.ConsumerConnectionPoint.ControlType.ToString() & _
"<br />" & _
" Allows multiple connections: " & _
conn.ConsumerConnectionPoint.AllowsMultipleConnections.ToString() & _
"<br />" & _
" Enabled: " & conn.ConsumerConnectionPoint.GetEnabled(zip1).ToString()
End Sub
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs)
lblConn.Text = String.Empty
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:WebPartManager ID="mgr" runat="server" >
<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="Dynamic Connection"
OnClick="Button1_Click" />
<br />
<asp:Button ID="Button2" runat="server"
Text="Connection Point Details"
OnClick="Button2_Click" />
<br />
<asp:Label ID="lblConn" runat="server" />
</div>
</form>
</body>
</html>
在瀏覽器中載入頁面之後,按兩下 [ 連接點詳細 數據] 按鈕。 宣告式連接中建立的提供者和取用者連接點相關信息隨即出現。 接下來,使用 [顯示模式 ] 下拉式控件,將頁面切換為連線模式。 在標題列) 的向下箭號所代表的 WebPart 其中一個 (控件動詞功能表上,單擊連接動詞。 連線 UI 隨即出現,由在頁面中宣告的 <asp:connectionszone>
控件自動建立。 按兩下 [ 中斷連線 ] 按鈕以終止現有的連線。 使用 [顯示模式 ] 控件,將頁面傳回流覽模式。 接下來,按兩下 [ 動態連線 ] 按鈕,以程式設計方式建立連線。 再次按下 [ 連接點詳細數據] 按鈕,以指出兩個連接點對象的詳細數據。
備註
每個網頁元件連線都包含兩個伺服器控制項共享數據:一個控件是取用者,另一個則是提供者。 如需網頁元件連接的基本元件和連接物件本身的討論,請參閱 WebPartConnection 類別概觀。 每個網頁元件連線都需要連接點。 提供者和取用者控件必須至少有一個定義的 ConnectionPoint 物件, (每個物件都可以選擇性地有多個連接點) ,其中包含控件如何連線至另一個控件的詳細數據及其可共用的數據類型。 在實際連接中,提供者有自己的連接點物件類型, (衍生自基 ConnectionPoint 類) 、 ProviderConnectionPoint 實例,而取用者有自己的物件,也就是 ConsumerConnectionPoint 實例。
若要建立提供者連接點,開發人員必須將回呼方法新增至提供者,以將實作的介面實例傳回給取用者。 它們必須使用程式代碼 ConnectionProvider
屬性標記原始程式碼中的回呼方法, (查看 ConnectionProviderAttribute 類別) 。 同樣地,若要建立取用者連接點,開發人員必須將方法新增至接收介面實例的取用者,而且必須將該方法標示 ConnectionConsumer
為屬性 (查看 ConnectionConsumerAttribute 類別) 。
當開發人員在網頁上建立連線時,可以建立為靜態或動態連線。 靜態連接會在網頁的標記中宣告。 宣告靜態連接時,開發人員可以藉由將值指派給ProviderConnectionPointID
元素標記內的 <asp:webpartconnection>
和 ConsumerConnectionPointID
屬性,來指定取用者和提供者所使用的連接點。 如果取用者和提供者控制項內定義了多個連接點,此方法特別有用,因為它可讓靜態連接識別要用於連線的連接點。
動態連線是由開發人員的程式代碼或使用者透過控制項提供 ConnectionsZone 的使用者介面 (UI) ,以程式設計方式建立。 若要在程式代碼中建立連接,開發人員必須在控件上WebPartManager呼叫 ConnectWebParts 方法,以取得 對象的實例WebPartConnection。 開發人員必須先參考取用者和提供者伺服器控制項及其各自的連接點物件,才能呼叫此方法。 若要取得提供者和取用者控件之連接點的參考,開發人員會先呼叫 GetProviderConnectionPoints 控件上的 WebPartManager 和 GetConsumerConnectionPoints 方法。 這些方法會傳回適當的連接點對象,接著可以傳遞至方法以建立連接。
當提供者和取用者的連接點物件都使用相同的介面類型時,它們相容,而且可以形成直接連線來共享數據。 如果它們無法與相同的介面類型搭配使用, WebPartTransformer 則必須使用 物件,將提供者中的介面實例轉譯成取用者可以使用的類型。
抽象 ConnectionPoint 類提供取用者和提供者控件所共用之連接點的基本詳細數據。 數個屬性包含這些詳細數據。 屬性 AllowsMultipleConnections 指出連接點一次是否可以參與多個連線。 根據預設,提供者連接點可以參與多個連線,而取用者連接點則無法參與。 屬性 ControlType 表示與連接點相關聯的伺服器控制項類型。 請注意,不只 WebPart 控件可以形成連線;任何伺服器控件,無論是 ASP.NET 控件、自定義控件或使用者控件,都可以在放置於 WebPartZoneBase 區域中時啟用參與連線。 屬性 DisplayName 會提供連接點的易記名稱,可在UI中顯示,以協助建立連線的使用者。 屬性 ID 可作為連接點物件本身的字串標識碼。 屬性 InterfaceType 會指出連接點所瞭解的介面實例類型。 指定的連接點是否提供或取用該介面的實例,取決於它是否為 ProviderConnectionPoint 或 ConsumerConnectionPoint 物件。
類別 ConnectionPoint 有一個方法。 方法 GetEnabled 會傳回布爾值,指出連接點目前是否能夠參與連線。
類別ConnectionPoint也有一個公用欄位。 DefaultID 此欄位包含用來識別連接中預設連接點的值。
注意
指定連接點方法的屬性只有一個必要參數, displayName
因此可以建立預設連接點屬性而不指定標識符。 在這種情況下, DefaultID 欄位會提供要使用的基底值。
欄位
DefaultID |
表示字串,用來識別與伺服器控制項關聯之連接點集合中的預設連接點。 |
屬性
AllowsMultipleConnections |
取得值,指出連接點是否支援多個同時連接。 |
ControlType |
取得與連接點關聯之伺服器控制項的 Type。 |
DisplayName |
取得字串,此字串在使用者介面 (UI) 做為表示連接點的易記顯示名稱。 |
ID |
取得字串,其中包含連接點的識別項。 |
InterfaceType |
取得連接點所使用的介面型別。 |
方法
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetEnabled(Control) |
傳回值,指出連接點是否可以參與連接。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |