WebPartManager.ConnectDisplayMode 欄位
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示用來顯示特別使用者介面 (UI) 的顯示模式,供使用者管理 WebPart 控制項之間的連接。 此欄位為唯讀。
public: static initonly System::Web::UI::WebControls::WebParts::WebPartDisplayMode ^ ConnectDisplayMode;
public static readonly System.Web.UI.WebControls.WebParts.WebPartDisplayMode ConnectDisplayMode;
staticval mutable ConnectDisplayMode : System.Web.UI.WebControls.WebParts.WebPartDisplayMode
Public Shared ReadOnly ConnectDisplayMode As WebPartDisplayMode
欄位值
範例
下列程式代碼範例示範模式的使用 ConnectDisplayMode 方式。
程式代碼範例有三個部分:
原始程序檔,其中包含可形成連接的介面和自定義 WebPart 控件。
提供連線UI並示範使用模式的 ConnectDisplayMode 網頁。
如何執行範例的說明。
程式代碼範例的第一個部分是原始程序檔,其中包含介面和兩個自定義 WebPart 控件的設計,以便連接它們。 若要執行程式碼範例,您必須編譯此原始程式碼。 您可以明確地編譯它,並將產生的元件放在網站的 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
範例的第二個部分是裝載自定義控件的網頁。 頁面上的伺服器 <script>
標籤內有數種方法,可將頁面上可用的顯示模式填入下拉式清單。 用戶可以從下拉式清單中選取這些專案,以變更頁面的顯示模式。 其中一種可用的顯示模式是連接顯示模式,因為 <asp:connectionszone>
元素是在頁面的標記中宣告。 請注意,這個專案不包含任何其他子專案;它只存在以啟用用戶的連線管理UI。
此 ConnectDisplayMode 模式會出現在此範例中的兩個位置。 首先,在方法中 Page_Init
,連接顯示模式會新增至顯示模式的下拉式清單,因為程式代碼會迴圈查看 屬性中所參考的 SupportedDisplayModes 集合。 其次,方法 Page_PreRender
會檢查頁面上目前的顯示模式,如果目前模式為 ConnectDisplayMode,則會在控件中 Label 顯示訊息。
<%@ Page Language="C#" %>
<%@ 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 Page_Init(object sender, EventArgs e)
{
foreach (WebPartDisplayMode mode in mgr.SupportedDisplayModes)
{
string modeName = mode.Name;
if (mode.IsEnabled(mgr))
{
ListItem item = new ListItem(modeName, modeName);
DisplayModeDropdown.Items.Add(item);
}
}
}
protected void DisplayModeDropdown_SelectedIndexChanged(object
sender, EventArgs e)
{
String selectedMode = DisplayModeDropdown.SelectedValue;
WebPartDisplayMode mode =
mgr.SupportedDisplayModes[selectedMode];
if (mode != null)
mgr.DisplayMode = mode;
}
protected void Page_PreRender(object sender, EventArgs e)
{
if (mgr.DisplayMode == WebPartManager.ConnectDisplayMode)
Label1.Visible = true;
else
Label1.Visible = false;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:WebPartManager ID="mgr" runat="server" />
<asp:Label ID="Label1" runat="server"
Text="Currently in Connect Display Mode"
Font-Bold="true"
Font-Size="125%" />
<br />
<asp:DropDownList ID="DisplayModeDropdown"
runat="server"
AutoPostBack="true"
Width="120"
OnSelectedIndexChanged=
"DisplayModeDropdown_SelectedIndexChanged">
</asp:DropDownList>
<hr />
<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" />
</div>
</form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ 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 Page_Init(ByVal sender As Object, _
ByVal e As EventArgs)
Dim mode As WebPartDisplayMode
For Each mode In mgr.SupportedDisplayModes
Dim modeName As String = mode.Name
If mode.IsEnabled(mgr) Then
Dim item As ListItem = New ListItem(modeName, modeName)
DisplayModeDropdown.Items.Add(item)
End If
Next
End Sub
Protected Sub DisplayModeDropdown_SelectedIndexChanged(ByVal _
sender As Object, ByVal e As EventArgs)
Dim selectedMode As String = _
DisplayModeDropdown.SelectedValue
Dim mode As WebPartDisplayMode = _
mgr.SupportedDisplayModes(selectedMode)
If mode IsNot Nothing Then
mgr.DisplayMode = mode
End If
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, _
ByVal e As System.EventArgs)
If mgr.DisplayMode.Equals(WebPartManager.ConnectDisplayMode) Then
Label1.Visible = True
Else
Label1.Visible = False
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">
<div>
<asp:WebPartManager ID="mgr" runat="server" />
<asp:Label ID="Label1" runat="server"
Text="Currently in Connect Display Mode"
Font-Bold="true"
Font-Size="125%" />
<br />
<asp:DropDownList ID="DisplayModeDropdown"
runat="server"
AutoPostBack="true"
Width="120"
OnSelectedIndexChanged=
"DisplayModeDropdown_SelectedIndexChanged">
</asp:DropDownList>
<hr />
<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" />
</div>
</form>
</body>
</html>
在瀏覽器中載入頁面之後,按下拉式清單,然後選取 [ 連線 ] 將頁面切換為連線顯示模式。 請注意,會出現一則訊息,告知頁面處於連線顯示模式。 現在,單擊動詞功能表 (其中一個 WebPart 控件標題欄中的箭頭符號) ,然後單擊動詞功能表中的 [ 連接 ]。 顯示連線 UI 之後,按兩下連結以建立連線。 使用出現的連線 UI 內的下拉式清單,選取將參與連線的其他控件,然後按兩下 [ 連線 ] 按鈕。 已建立連線。 按兩下 [ 關閉 ] 按鈕,然後使用頁面頂端的下拉式清單來傳回頁面以瀏覽顯示模式。
備註
欄位ConnectDisplayMode會參考 控件所建立和包含的WebPartManager自定義WebPartDisplayMode物件。 因為這是靜態物件,所以您可以直接透過 WebPartManager 類別參考它,而不需要控件的實例。
當使用者想要管理網頁上控件之間的 WebPart 連線時,如果 ConnectionsZone 頁面上已宣告區域,他們可以將頁面切換為 ConnectDisplayMode 模式。 線上顯示模式會顯示用於管理連線的特殊UI,包括連接或中斷連線機控制元件的能力,以及編輯現有連線的詳細數據。
如果您想要讓用戶能夠管理 Web 元件控制項集所提供 UI 的連線,您必須在頁面的標記中宣告 <asp:connectionszone>
元素。 不同於其他類型的 WebZone 區域元素,您不需要在此元素內新增任何其他標記;您只需自行宣告元素即可。