WebPartDisplayMode 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
為 Web 組件頁面可以進入的多種顯示模式,定義通用的屬性集。
public ref class WebPartDisplayMode abstract
public abstract class WebPartDisplayMode
type WebPartDisplayMode = class
Public MustInherit Class WebPartDisplayMode
- 繼承
-
WebPartDisplayMode
範例
下列程式代碼範例示範網頁元件頁面上顯示模式的宣告式用法。 每個顯示模式都是由 Web 元件控制項集所實作,衍生自 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
程式代碼範例的第二個部分是參考元素內<asp:webpartzone>
標準 ASP.NET Calendar 控件的網頁,讓控件在運行時間以GenericWebPart控件包裝,並提供基本的網頁元件功能。 頁面也會參考 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.ascx 或 DisplayModeMenuVB.asc (x 的檔案中,視您用於程式代碼範例的語言) 而定,並將它放在與網頁相同的目錄中。 如需此控件中顯示模式和原始程式碼描述的詳細資訊,請參閱逐步解說 :變更網頁元件頁面上的顯示模式主題。
<%@ 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
控件新增至頁面。
備註
網頁元件頁面可以進入數種不同的顯示模式。 在每個顯示模式中,網頁元件使用者介面的某些元素 (UI) 都會隱藏或顯示,而且啟用或停用特定類型的使用者修改頁面。 控件 WebPartManager 包含網頁元件控件集中可用的顯示模式實作,並管理頁面的顯示模式。
下表列出代表可用顯示模式的欄位。
顯示模式 | 描述 |
---|---|
BrowseDisplayMode | 在用戶檢視頁面的一般模式中顯示網頁元件控制件和UI元素。 |
DesignDisplayMode | 顯示區域UI元素,並讓使用者拖曳Web元件控制項以變更頁面的配置。 |
EditDisplayMode | 顯示特殊的編輯 UI 元素,並可讓使用者編輯頁面上的控件。 |
CatalogDisplayMode | 顯示特殊的目錄 UI 元素,並讓使用者新增和移除頁面控件。 |
ConnectDisplayMode | 顯示特殊連線 UI 元素,並讓使用者能夠連接網頁元件控制件。 |
給實施者的注意事項
開發人員可以從類別衍生 WebPartDisplayMode 來建立自定義顯示模式。 若要在網頁元件頁面上提供自定義 WebPartDisplayMode ,您也需要衍生自 WebPartManager 類別,並覆寫其 CreateDisplayModes() 方法。
建構函式
WebPartDisplayMode(String) |
初始化顯示模式名稱的值。 |
屬性
AllowPageDesign |
取得值,判斷當網頁為特定顯示模式時,使用者是否可以變更 Web 組件頁面的配置。 |
AssociatedWithToolZone |
取得值,表示特定顯示模式是否與衍生自 ToolZone 類別的類別關聯。 |
Name |
取得顯示模式名稱。 |
RequiresPersonalization |
取得值,表示特定顯示模式是否需要可以個人化才能啟動。 |
ShowHiddenWebParts |
取得值,表示是否應該顯示 Hidden 屬性設為 |
方法
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
IsEnabled(WebPartManager) |
取得值,表示網頁為特定顯示模式時,使用者是否可以個人化網頁。 |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |