WebPartDisplayMode 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
定義了網頁部分頁面可進入的多種顯示模式的共通屬性集合。
public ref class WebPartDisplayMode abstract
public abstract class WebPartDisplayMode
type WebPartDisplayMode = class
Public MustInherit Class WebPartDisplayMode
- 繼承
-
WebPartDisplayMode
範例
以下程式碼範例展示了在網頁部分頁面上宣告式使用顯示模式的方式。 這些顯示模式,由 Web Parts 控制集實作,皆源自類別 WebPartDisplayMode 。
此程式碼範例包含四個部分:
自訂 WebPart 控制。
一個網頁,裡面有區域來承載自訂控制。
一個使用者控制裝置,允許使用者在網頁上切換顯示模式。
說明該頁面在瀏覽器中的運作方式。
範例的第一部分是一個自訂WebPart控制項。 TextDisplayWebPart 要執行程式碼範例,你必須編譯這個原始碼。 你可以明確編譯,並將結果組合放到你網站的 Bin 資料夾或全域組合快取中。 或者,你也可以把原始碼放在網站的App_Code資料夾,執行時會動態編譯。 關於兩種編譯方法的示範,請參見《 攻略:開發與使用自訂網頁伺服器控制項》。
using System;
using System.Security.Permissions;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace Samples.AspNet.CS.Controls
{
[AspNetHostingPermission(SecurityAction.Demand,
Level=AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level=AspNetHostingPermissionLevel.Minimal)]
public class TextDisplayWebPart : WebPart
{
private String _contentText = null;
TextBox input;
Label DisplayContent;
const string _subTitle = "Contoso, Ltd";
public TextDisplayWebPart()
{
this.AllowClose = false;
}
[
Personalizable(PersonalizationScope.User, true),
WebBrowsable()
]
public String ContentText
{
get { return _contentText; }
set { _contentText = value; }
}
protected override void CreateChildControls()
{
Controls.Clear();
DisplayContent = new Label();
DisplayContent.BackColor =
System.Drawing.Color.LightBlue;
DisplayContent.Text = this.ContentText;
this.Controls.Add(DisplayContent);
input = new TextBox();
this.Controls.Add(input);
Button update = new Button();
update.Text = "Set Label Content";
update.Click += new EventHandler(this.submit_Click);
this.Controls.Add(update);
ChildControlsCreated = true;
}
private void submit_Click(object sender, EventArgs e)
{
// Update the label string.
if (!string.IsNullOrEmpty(input.Text))
{
this.ContentText = Page.Server.HtmlEncode(input.Text) + @"<br />";
// Clear the input textbox.
input.Text = String.Empty;
DisplayContent.Text = this.ContentText;
}
}
}
}
Imports System.Security.Permissions
Imports System.Web
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 Class TextDisplayWebPart
Inherits WebPart
Private _contentText As String = Nothing
Private input As TextBox
Private DisplayContent As Label
Public Sub New()
Me.AllowClose = False
End Sub
<Personalizable(), WebBrowsable()> _
Public Property ContentText() As String
Get
Return _contentText
End Get
Set
_contentText = value
End Set
End Property
Protected Overrides Sub CreateChildControls()
Controls.Clear()
DisplayContent = New Label()
DisplayContent.Text = Me.ContentText
DisplayContent.BackColor = _
System.Drawing.Color.LightBlue
Me.Controls.Add(DisplayContent)
input = New TextBox()
Me.Controls.Add(input)
Dim update As New Button()
update.Text = "Set Label Content"
AddHandler update.Click, AddressOf Me.submit_Click
Me.Controls.Add(update)
ChildControlsCreated = True
End Sub
Private Sub submit_Click(ByVal sender As Object, _
ByVal e As EventArgs)
' Update the label string.
If input.Text <> String.Empty Then
Me.ContentText = Page.Server.HtmlEncode(input.Text) + "<br />"
' Clear the input textbox.
input.Text = String.Empty
DisplayContent.Text = Me.ContentText
End If
End Sub
End Class
End Namespace
程式碼範例的第二部分是一個網頁,在 Calendar 元素中參考標準的 ASP.NET <asp:webpartzone> 控制項,使該控制項被 GenericWebPart 控制項包裹,並在執行時提供基本的 Web Parts 功能。 頁面也會參考 TextDisplayWebPart 元素中的 <asp:catalogzone> 控制項,展示終端使用者切換到目錄模式並新增控制項的能力。 頁面還包含一個 <asp:editorzone> 元素,讓使用者在頁面處於編輯模式時能編輯所 <asp:webpartzone> 包含的控制項。 頁面上方有 register 自訂控制項的指令,還有使用者控制的指令。
<%@ page language="C#" %>
<%@ register TagPrefix="uc1"
TagName="DisplayModeMenuCS"
Src="DisplayModeMenuCS.ascx" %>
<%@ register tagprefix="aspSample"
Namespace="Samples.AspNet.CS.Controls"
Assembly="TextDisplayWebPartCS"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Button1_Click(object sender, EventArgs e)
{
WebPartManager1.DisplayMode = WebPartManager.CatalogDisplayMode;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Web Parts Display Modes</title>
</head>
<body>
<form id="Form2" runat="server">
<asp:webpartmanager id="WebPartManager1" runat="server" />
<uc1:DisplayModeMenuCS ID="DisplayModeMenu1" runat="server" />
<asp:webpartzone
id="WebPartZone1"
runat="server" BackImageUrl="~/MyImage.gif">
<zonetemplate>
<asp:Calendar
ID="Calendar1"
Runat="server"
Title="My Calendar" />
</zonetemplate>
</asp:webpartzone>
<asp:WebPartZone ID="WebPartZone2" Runat="server">
</asp:WebPartZone>
<asp:EditorZone ID="editzone1" Runat="server">
<ZoneTemplate>
<asp:AppearanceEditorPart
ID="appearanceeditor1"
Runat="server" />
<asp:LayoutEditorPart
ID="LayoutEditorPart1"
Runat="server" />
</ZoneTemplate>
</asp:EditorZone>
<asp:CatalogZone ID="catalogzone1" Runat="server">
<ZoneTemplate>
<asp:DeclarativeCatalogPart
ID="declarativepart1"
Runat="server">
<WebPartsTemplate>
<aspSample:TextDisplayWebPart
runat="server"
id="textwebpart"
title = "Text Content WebPart" AllowClose="true"/>
</WebPartsTemplate>
</asp:DeclarativeCatalogPart>
</ZoneTemplate>
</asp:CatalogZone>
<br />
<asp:button
id="button1"
runat="server"
text="Catalog Mode"
OnClick="Button1_Click"
/>
</form>
</body>
</html>
<%@ page language="VB" %>
<%@ register TagPrefix="uc1"
TagName="DisplayModeMenuVB"
Src="DisplayModeMenuVB.ascx" %>
<%@ register tagprefix="aspSample"
Namespace="Samples.AspNet.VB.Controls"
Assembly="TextDisplayWebPartVB"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Sub Button1_Click(Byval sender As Object, _
ByVal e As EventArgs)
WebPartManager1.DisplayMode = WebPartManager.CatalogDisplayMode
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Web Parts Display Modes</title>
</head>
<body>
<form id="Form2" runat="server">
<asp:webpartmanager id="WebPartManager1" runat="server" />
<uc1:DisplayModeMenuVB ID="DisplayModeMenu1" runat="server" />
<asp:webpartzone
id="WebPartZone1"
runat="server" BackImageUrl="~/MyImage.gif">
<zonetemplate>
<asp:Calendar
ID="Calendar1"
Runat="server"
Title="My Calendar" />
</zonetemplate>
</asp:webpartzone>
<asp:WebPartZone ID="WebPartZone2" Runat="server">
</asp:WebPartZone>
<asp:EditorZone ID="editzone1" Runat="server">
<ZoneTemplate>
<asp:AppearanceEditorPart
ID="appearanceeditor1"
Runat="server" />
<asp:LayoutEditorPart
ID="LayoutEditorPart1"
Runat="server" />
</ZoneTemplate>
</asp:EditorZone>
<asp:CatalogZone ID="catalogzone1" Runat="server">
<ZoneTemplate>
<asp:DeclarativeCatalogPart
ID="declarativepart1"
Runat="server">
<WebPartsTemplate>
<aspSample:TextDisplayWebPart
runat="server"
id="textwebpart"
title = "Text Content WebPart"/>
</WebPartsTemplate>
</asp:DeclarativeCatalogPart>
</ZoneTemplate>
</asp:CatalogZone>
<br />
<asp:button
id="button1"
runat="server"
text="Catalog Mode"
OnClick="Button1_Click"
/>
</form>
</body>
</html>
程式碼範例的第三部分是一個使用者控制裝置,允許使用者在網頁上切換顯示模式。 將此控制項的原始碼儲存在一個名為 DisplayModeMenuCS.ascx 或 DisplayModeMenuVB.ascx(依你範例所用語言而定)的檔案中,並放在與網頁相同的目錄中。 欲了解更多顯示模式細節及本控制項原始碼說明,請參閱主題「 攻略:在網頁零件頁面更改顯示模式」。
<%@ 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>
當你在瀏覽器載入頁面時,可以使用 顯示模式 下拉清單控制來切換到不同的顯示模式。 要編輯控制項,請在下拉選單控制項中選擇 「編輯 」。 要編輯特定控制項,請點擊控制項標題欄中的箭頭,然後在動詞選單中點擊 編輯 ,以暴露其動詞選單。 當控制項處於編輯模式時,新增到此頁面的編輯控制項可讓您更改已編輯控制項的外觀與版面配置。 完成後,請在顯示模式下拉選單控制中選擇瀏覽,將頁面恢復為正常視圖。 若要在頁面上新增控制,請切換到目錄模式。 請注意,你可以使用 顯示模式 的下拉選單控制,或點擊頁面底部附近的按鈕。 此 Button1_Click 方法的內嵌程式碼示範如何程式化切換顯示模式。 在目錄模式下,你可以在頁面上新增自訂 TextDisplayWebPart 控制項。
備註
網頁部分頁面可以進入多種不同的顯示模式。 在每種顯示模式下,Web Parts 使用者介面(UI)的某些元素會被隱藏或顯示,且某些使用者對頁面的修改會被啟用或停用。 控制 WebPartManager 項包含 Web Parts 控制項集中可用的顯示模式實作,並管理頁面的顯示模式。
下表列出代表可用顯示模式的欄位。
| 顯示模式 | Description |
|---|---|
| BrowseDisplayMode | 以一般模式顯示網頁零件控制項與使用者介面元素,最終使用者可瀏覽頁面。 |
| DesignDisplayMode | 顯示區域介面元素,並允許使用者拖曳網頁零件控制項以更改頁面版面配置。 |
| EditDisplayMode | 顯示特殊的編輯介面元素,並允許終端使用者編輯頁面上的控制項。 |
| CatalogDisplayMode | 顯示特殊的目錄介面元素,並允許終端使用者新增或移除頁面控制項。 |
| ConnectDisplayMode | 顯示特殊連接介面元素,並允許終端使用者連接網頁零件控制項。 |
給實施者的注意事項
開發者可以從這個 WebPartDisplayMode 類別衍生出自訂的顯示模式。 要讓自訂 WebPartDisplayMode 檔案在網頁上可用,你也需要從該 WebPartManager 類別衍生並覆寫其 CreateDisplayModes() 方法。
建構函式
| 名稱 | Description |
|---|---|
| WebPartDisplayMode(String) |
初始化顯示模式名稱的值。 |
屬性
| 名稱 | Description |
|---|---|
| AllowPageDesign |
取得一個值,決定使用者在網頁處於特定顯示模式時,是否能更改網頁部分頁面的版面配置。 |
| AssociatedWithToolZone |
會得到一個值,表示某個顯示模式是否與該 ToolZone 類別衍生的類別相關聯。 |
| Name |
會被命名為顯示模式。 |
| RequiresPersonalization |
會獲得一個值,指示特定顯示模式是否需要啟用個人化。 |
| ShowHiddenWebParts |
會得到一個值,指示是否應該顯示那些屬性設定 Hidden 為 的 |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| IsEnabled(WebPartManager) |
會有一個值,表示使用者在頁面處於特定顯示模式時,是否能個人化頁面。 |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
傳回表示目前 物件的字串。 (繼承來源 Object) |