WebPartManager.DisconnectWebParts(WebPartConnection) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
對網頁上連接的伺服器控制項,執行中斷連接的程序。
public:
virtual void DisconnectWebParts(System::Web::UI::WebControls::WebParts::WebPartConnection ^ connection);
public virtual void DisconnectWebParts (System.Web.UI.WebControls.WebParts.WebPartConnection connection);
abstract member DisconnectWebParts : System.Web.UI.WebControls.WebParts.WebPartConnection -> unit
override this.DisconnectWebParts : System.Web.UI.WebControls.WebParts.WebPartConnection -> unit
Public Overridable Sub DisconnectWebParts (connection As WebPartConnection)
參數
- connection
- WebPartConnection
WebPartConnection,表示伺服器控制項之間的連接。
例外狀況
connection
為 null
。
connection
不包含在 StaticConnections 或 DynamicConnections。
StaticConnections 是唯讀的。
-或-
connection
已從 StaticConnections 中斷連接。
-或-
DynamicConnections 是唯讀的。
-或-
connection
已從 DynamicConnections 中斷連接。
範例
下列程式碼範例會示範如何使用 DisconnectWebParts 方法。 使用兩個自定義 WebPart 控件,網頁可讓您按下按鈕來建立控件之間的連線,而另一個按鈕可讓您中斷控件的連線。
程式代碼範例有四個部分:
用於變更顯示模式的使用者控制件。
包含自定義 WebPart 控件的來源檔案。
用來裝載控件的網頁。
說明範例如何在瀏覽器中運作。
程式代碼範例的第一個部分是用來變更顯示模式的使用者控件。 您可以從類別概觀的 WebPartManager 範例區段取得使用者控制項的原始碼。 如需顯示模式和使用者控件運作方式的詳細資訊,請參閱 逐步解說:變更網頁元件頁面上的顯示模式。
第二個部分是一個檔案,其中包含要連接的兩個自定義 WebPart 控件的原始程式碼。 若要執行程式碼範例,您必須編譯此原始程式碼。 您可以明確地編譯它,並將產生的元件放在網站的 Bin 資料夾或全域程式集緩存中。 或者,您也可以將原始程式碼放在月臺的 App_Code資料夾中,在運行時間會動態編譯原始程式碼。 這個範例會使用動態編譯,因此 Register
參考網頁中這些元件的指示詞會據以宣告在網頁頂端。 如需示範編譯選項的逐步解說,請參閱逐步解說 :開發和使用自定義網頁伺服器控件。
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
程式代碼範例的第三個部分是網頁。 請注意,在頂端附近,它包含 Register
指示詞,可向控件註冊用戶控件和動態編譯的 WebPart 元件。 頁面有兩個主要方法。 方法 Button1_Click
會在控件之間建立連接,而 Button2_Click
方法會中斷控件的連接。
<%@ 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 Button2_Click(object sender, EventArgs e)
{
if (mgr.Connections.Count >= 1 && mgr.Connections[0] != null)
mgr.DisconnectWebParts(mgr.Connections[0]);
}
</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">
</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" />
<asp:Button ID="Button2" runat="server"
Text="Disconnect WebPart Controls"
OnClick="Button2_Click" />
</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 mgr As WebPartManager = _
WebPartManager.GetCurrentWebPartManager(Page)
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 Button2_Click(ByVal sender as Object, _
ByVal e as System.EventArgs)
If mgr.Connections.Count >= 1 AndAlso _
mgr.Connections(0) IsNot Nothing Then
mgr.DisconnectWebParts(mgr.Connections(0))
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">
</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" />
<asp:Button ID="Button2" runat="server"
Text="Disconnect WebPart Controls"
OnClick="Button2_Click" />
</div>
</form>
</body>
</html>
載入頁面之後,您可以按兩下 [ 連線 ] 按鈕來連接控制件。 如果您在文字框控制件中輸入一些文字,然後按下 [輸入 ] 按鈕,文字將會顯示在連接的控件中, (如果控件已中斷連線,則不會顯示該文字) 。 如果您按兩下 [ 中斷連線 ] 按鈕,控制件將會中斷連線。 您可以使用 [顯示模式 ] 下拉式清單控件,將頁面切換為 [連線 ] 模式,來驗證控件的連線狀態。 執行此動作之後,按兩下其中一個控件標題列中的箭號) 所代表的動詞功能表 (,然後選取 [連接 ] 專案。 隨即顯示連線 UI;它可供使用,因為 <asp:connectionszone>
頁面中有宣告的專案。 您也可以從此 UI 連線和中斷控件連線。
備註
當您傳遞 connection
參數時,方法DisconnectWebParts會執行結束或伺服器控制件之間WebPart連線的完整程式。
當您將元素放在 <asp:connectionszone>
網頁時,這個方法可用來中斷控件的連線,以提供使用者介面 (UI) 來管理連線。 當頁面處於連線顯示模式 (ConnectDisplayMode) ,且目前連線存在時,使用者可以按兩下呼叫 方法來結束連線的按鈕 DisconnectWebParts 。
如果您想要以程式設計方式中斷控件的連線,而不需要將元素新增<asp:connectionszone>
至頁面,您也可以直接從程式代碼呼叫 DisconnectWebParts 方法。
給繼承者的注意事項
如果您想要變更中斷WebPart連線控件的默認實作,可以覆寫 DisconnectWebParts(WebPartConnection) 方法。 如果您確實覆寫 方法,而且只要將一些實作新增至現有的方法,就可以在執行自己的程序代碼之前呼叫基底方法。