IWebEditable 介面
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供介面給開發人員,以指定與 WebPart 控制項相關聯的自訂編輯控制項。
public interface class IWebEditable
public interface IWebEditable
type IWebEditable = interface
Public Interface IWebEditable
- 衍生
範例
下列程式碼範例示範如何在自訂 WebPart 控制項中覆寫 介面的方法 IWebEditable ,然後在控制項進入編輯模式時 WebPart 建立自訂 EditorPart 控制項的實例。
此範例有四個部分:
自訂類別的程式碼。
裝載自訂控制項的網頁。
將頁面切換為編輯模式的使用者控制項。
範例在瀏覽器中運作方式的描述。
程式碼範例的第一個部分是自訂 TextDisplayWebPart
類別。 請注意,類別衍生自 WebPart 類別,並實 IWebEditable 作 介面,提供方法和 WebBrowsableObject 屬性的特定實作 CreateEditorParts 。 另請注意,在 類別內 TextDisplayWebPart
巢狀是衍生自基 EditorPart 類的私用自訂 TextDisplayEditorPart
類別。 若要執行程式碼範例,您必須編譯此原始程式碼。 您可以明確地編譯它,並將產生的元件放在網站的 Bin 資料夾或全域組件快取中。 或者,您可以將原始程式碼放在月臺的 App_Code 資料夾中,其將在執行時間動態編譯。 如需示範這兩種編譯方法的逐步解說,請參閱逐步解說 :開發和使用自訂 Web 服務器控制項。
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
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;
private String _fontStyle = null;
TextBox input;
Label DisplayContent;
Literal lineBreak;
public override EditorPartCollection CreateEditorParts()
{
ArrayList editorArray = new ArrayList();
TextDisplayEditorPart edPart = new TextDisplayEditorPart();
edPart.ID = this.ID + "_editorPart1";
editorArray.Add(edPart);
EditorPartCollection editorParts =
new EditorPartCollection(editorArray);
return editorParts;
}
public override object WebBrowsableObject
{
get { return this; }
}
[Personalizable(), WebBrowsable]
public String ContentText
{
get { return _contentText; }
set { _contentText = value; }
}
[Personalizable(), WebBrowsable()]
public String FontStyle
{
get { return _fontStyle; }
set { _fontStyle = value; }
}
protected override void CreateChildControls()
{
Controls.Clear();
DisplayContent = new Label();
DisplayContent.BackColor = Color.LightBlue;
DisplayContent.Text = this.ContentText;
if (FontStyle == null)
FontStyle = "None";
SetFontStyle(DisplayContent, FontStyle);
this.Controls.Add(DisplayContent);
lineBreak = new Literal();
lineBreak.Text = @"<br />";
Controls.Add(lineBreak);
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);
}
private void submit_Click(object sender, EventArgs e)
{
// Update the label string.
if (!string.IsNullOrEmpty(input.Text))
{
_contentText = input.Text + @"<br />";
input.Text = String.Empty;
DisplayContent.Text = this.ContentText;
}
}
private void SetFontStyle(Label label, String selectedStyle)
{
if (selectedStyle == "Bold")
{
label.Font.Bold = true;
label.Font.Italic = false;
label.Font.Underline = false;
}
else if (selectedStyle == "Italic")
{
label.Font.Italic = true;
label.Font.Bold = false;
label.Font.Underline = false;
}
else if (selectedStyle == "Underline")
{
label.Font.Underline = true;
label.Font.Bold = false;
label.Font.Italic = false;
}
else
{
label.Font.Bold = false;
label.Font.Italic = false;
label.Font.Underline = false;
}
}
// Create a custom EditorPart to edit the WebPart control.
[AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal)]
private class TextDisplayEditorPart : EditorPart
{
DropDownList _partContentFontStyle;
public override bool ApplyChanges()
{
TextDisplayWebPart part =
(TextDisplayWebPart)WebPartToEdit;
// Update the custom WebPart control with the font style.
part.FontStyle = PartContentFontStyle.SelectedValue;
return true;
}
public override void SyncChanges()
{
TextDisplayWebPart part =
(TextDisplayWebPart)WebPartToEdit;
String currentStyle = part.FontStyle;
// Select the current font style in the drop-down control.
foreach (ListItem item in PartContentFontStyle.Items)
{
if (item.Value == currentStyle)
{
item.Selected = true;
break;
}
}
}
protected override void CreateChildControls()
{
Controls.Clear();
// Add a set of font styles to the dropdown list.
_partContentFontStyle = new DropDownList();
_partContentFontStyle.Items.Add("Bold");
_partContentFontStyle.Items.Add("Italic");
_partContentFontStyle.Items.Add("Underline");
_partContentFontStyle.Items.Add("None");
Controls.Add(_partContentFontStyle);
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("<b>Text Content Font Style</b>");
writer.WriteBreak();
writer.Write("Select a font style.");
writer.WriteBreak();
_partContentFontStyle.RenderControl(writer);
writer.WriteBreak();
}
// Access the drop-down control through a property.
private DropDownList PartContentFontStyle
{
get
{
EnsureChildControls();
return _partContentFontStyle;
}
}
}
}
}
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
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 _fontStyle As String = Nothing
Private input As TextBox
Private DisplayContent As Label
Private lineBreak As Literal
Public Overrides Function CreateEditorParts() _
As EditorPartCollection
Dim editorArray As New ArrayList()
Dim edPart as New TextDisplayEditorPart()
edPart.ID = Me.ID & "_editorPart1"
editorArray.Add(edPart)
Dim editorParts As New EditorPartCollection(editorArray)
Return editorParts
End Function
Public Overrides ReadOnly Property WebBrowsableObject() _
As Object
Get
Return Me
End Get
End Property
<Personalizable(), WebBrowsable()> _
Public Property ContentText() As String
Get
Return _contentText
End Get
Set(ByVal value As String)
_contentText = Value
End Set
End Property
<Personalizable(), WebBrowsable()> _
Public Property FontStyle() As String
Get
Return _fontStyle
End Get
Set(ByVal value As String)
_fontStyle = Value
End Set
End Property
Protected Overrides Sub CreateChildControls()
Controls.Clear()
DisplayContent = New Label()
DisplayContent.BackColor = Color.LightBlue
DisplayContent.Text = Me.ContentText
If FontStyle Is Nothing Then
FontStyle = "None"
End If
SetFontStyle(DisplayContent, FontStyle)
Me.Controls.Add(DisplayContent)
lineBreak = New Literal()
lineBreak.Text = "<br />"
Controls.Add(lineBreak)
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)
End Sub
Private Sub submit_Click(ByVal sender As Object, _
ByVal e As EventArgs)
' Update the label string.
If input.Text <> String.Empty Then
_contentText = input.Text + "<br />"
input.Text = String.Empty
DisplayContent.Text = Me.ContentText
End If
End Sub
Private Sub SetFontStyle(ByVal label As Label, _
ByVal selectedStyle As String)
If selectedStyle = "Bold" Then
label.Font.Bold = True
label.Font.Italic = False
label.Font.Underline = False
ElseIf selectedStyle = "Italic" Then
label.Font.Italic = True
label.Font.Bold = False
label.Font.Underline = False
ElseIf selectedStyle = "Underline" Then
label.Font.Underline = True
label.Font.Bold = False
label.Font.Italic = False
Else
label.Font.Bold = False
label.Font.Italic = False
label.Font.Underline = False
End If
End Sub
' Create a custom EditorPart to edit the WebPart control.
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Private Class TextDisplayEditorPart
Inherits EditorPart
Private _partContentFontStyle As DropDownList
Public Overrides Function ApplyChanges() As Boolean
Dim part As TextDisplayWebPart = CType(WebPartToEdit, _
TextDisplayWebPart)
' Update the custom WebPart control with the font style.
part.FontStyle = PartContentFontStyle.SelectedValue
Return True
End Function
Public Overrides Sub SyncChanges()
Dim part As TextDisplayWebPart = CType(WebPartToEdit, _
TextDisplayWebPart)
Dim currentStyle As String = part.FontStyle
' Select the current font style in the drop-down control.
Dim item As ListItem
For Each item In PartContentFontStyle.Items
If item.Value = currentStyle Then
item.Selected = True
Exit For
End If
Next item
End Sub
Protected Overrides Sub CreateChildControls()
Controls.Clear()
' Add a set of font styles to the dropdown list.
_partContentFontStyle = New DropDownList()
_partContentFontStyle.Items.Add("Bold")
_partContentFontStyle.Items.Add("Italic")
_partContentFontStyle.Items.Add("Underline")
_partContentFontStyle.Items.Add("None")
Controls.Add(_partContentFontStyle)
End Sub
Protected Overrides Sub RenderContents(ByVal writer _
As HtmlTextWriter)
writer.Write("<b>Text Content Font Style</b>")
writer.WriteBreak()
writer.Write("Select a font style.")
writer.WriteBreak()
_partContentFontStyle.RenderControl(writer)
writer.WriteBreak()
End Sub
' Access the drop-down control through a property.
Private ReadOnly Property PartContentFontStyle() As DropDownList
Get
EnsureChildControls()
Return _partContentFontStyle
End Get
End Property
End Class
End Class
End Namespace
程式碼範例的第二個部分是裝載自訂控制項的網頁。 請注意,雖然 EditorZone 控制項是在頁面的標記中宣告,但是 EditorPart 自訂控制項不需要在該處參考,因為它可以在執行時間以程式設計方式新增。
<%@ page language="c#" %>
<%@ register TagPrefix="uc1"
TagName="DisplayModeUC"
Src="DisplayModeUCcs.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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>
Text Display WebPart with EditorPart
</title>
</head>
<body>
<form id="form1" runat="server">
<asp:webpartmanager id="WebPartManager1" runat="server" />
<uc1:DisplayModeUC ID="DisplayModeUC1" runat="server" />
<asp:webpartzone id="zone1" runat="server"
CloseVerb-Enabled="false">
<zonetemplate>
<aspSample:TextDisplayWebPart
runat="server"
id="textwebpart"
title = "Text Content WebPart" />
</zonetemplate>
</asp:webpartzone>
<asp:EditorZone ID="EditorZone1" runat="server" />
</form>
</body>
</html>
<%@ page language="vb" %>
<%@ register TagPrefix="uc1"
TagName="DisplayModeUC"
Src="DisplayModeUCvb.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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>
Text Display WebPart with EditorPart
</title>
</head>
<body>
<form id="form1" runat="server">
<asp:webpartmanager id="WebPartManager1" runat="server" />
<uc1:DisplayModeUC ID="DisplayModeUC1" runat="server" />
<asp:webpartzone id="zone1" runat="server"
CloseVerb-Enabled="false">
<zonetemplate>
<aspSample:TextDisplayWebPart
runat="server"
id="textwebpart"
title = "Text Content WebPart" />
</zonetemplate>
</asp:webpartzone>
<asp:EditorZone ID="EditorZone1" runat="server" />
</form>
</body>
</html>
程式碼範例的第三個部分是使用者控制項,可讓使用者將頁面切換為編輯模式。 請注意,主控網頁中會參考使用者控制項。 如需如何建立此使用者控制項的完整描述,請參閱逐步解說 :變更網頁元件頁面上的顯示模式。
<%@ control language="C#" classname="DisplayModeMenu"%>
<script runat="server">
// On initial load, fill the dropdown with display modes.
void DisplayModeDropdown_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
WebPartManager mgr =
WebPartManager.GetCurrentWebPartManager(Page);
String browseModeName = WebPartManager.BrowseDisplayMode.Name;
// Use a sorted list so the modes are sorted alphabetically.
SortedList itemArray =
new SortedList(mgr.SupportedDisplayModes.Count);
// Add display modes only if they are supported on the page.
foreach (WebPartDisplayMode mode in mgr.SupportedDisplayModes)
{
String modeName = mode.Name;
itemArray.Add(modeName, modeName + " Mode");
}
// Fill the dropdown with the display mode names.
foreach(DictionaryEntry arrayItem in itemArray)
{
ListItem item = new ListItem(arrayItem.Value.ToString(),
arrayItem.Key.ToString());
if (item.Value == browseModeName)
item.Selected = true;
DisplayModeDropdown.Items.Add(item);
}
}
}
// Change the page to the selected display mode.
void DisplayModeDropdown_SelectedIndexChanged(object sender,
EventArgs e)
{
WebPartManager mgr = WebPartManager.GetCurrentWebPartManager(Page);
String selectedMode = DisplayModeDropdown.SelectedValue;
foreach (WebPartDisplayMode mode in mgr.SupportedDisplayModes)
{
if (selectedMode == mode.Name)
{
mgr.DisplayMode = mode;
break;
}
}
}
</script>
<div>
<asp:DropDownList ID="DisplayModeDropdown"
runat="server"
AutoPostBack="true"
OnLoad="DisplayModeDropdown_Load"
OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" />
</div>
<%@ control language="vb" classname="DisplayModeMenu"%>
<script runat="server">
' On initial load, fill the dropdown with display modes.
Sub DisplayModeDropdown_Load(ByVal sender As Object, _
ByVal e As System.EventArgs)
If Not IsPostBack Then
Dim mgr As WebPartManager = _
WebPartManager.GetCurrentWebPartManager(Page)
Dim browseModeName As String = _
WebPartManager.BrowseDisplayMode.Name
' Use a sorted list so the modes are sorted alphabetically.
Dim itemArray As New SortedList(mgr.SupportedDisplayModes.Count)
' Add display modes only if they are supported on the page.
Dim mode As WebPartDisplayMode
For Each mode In mgr.SupportedDisplayModes
Dim modeName As String = mode.Name
itemArray.Add(modeName, modeName + " Mode")
Next mode
' Fill the dropdown with the display mode names.
Dim arrayItem As DictionaryEntry
For Each arrayItem In itemArray
Dim item As New ListItem(arrayItem.Value.ToString(), _
arrayItem.Key.ToString())
If item.Value = browseModeName Then
item.Selected = True
End If
DisplayModeDropdown.Items.Add(item)
Next arrayItem
End If
End Sub
' Change the page to the selected display mode.
Sub DisplayModeDropdown_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As EventArgs)
Dim mgr As WebPartManager = _
WebPartManager.GetCurrentWebPartManager(Page)
Dim selectedMode As String = DisplayModeDropdown.SelectedValue
Dim mode As WebPartDisplayMode
For Each mode In mgr.SupportedDisplayModes
If selectedMode = mode.Name Then
mgr.DisplayMode = mode
Exit For
End If
Next mode
End Sub
</script>
<div>
<asp:DropDownList ID="DisplayModeDropdown"
runat="server"
AutoPostBack="true"
OnLoad="DisplayModeDropdown_Load"
OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" />
</div>
若要執行程式碼範例,請在瀏覽器中載入主控網頁、將一些文字新增至文字方塊,然後按一下 [ 設定標籤內容 ] 按鈕以更新控制項中的標籤。 若要將頁面切換為編輯模式,請從包含顯示模式的下拉式清單中選取 [ 編輯 ]。 若要從自訂 TextDisplayEditorPart
控制項顯示 UI,請按一下控制項上的 TextDisplayWebPart
動詞功能表下拉式箭號,然後選取 [ 編輯]。 在編輯 UI 中,您可以使用包含字型樣式的下拉式清單來更新控制項中標籤的 TextDisplayWebPart
文字樣式。 您必須在顯示模式下拉式清單中按一下 [瀏覽模式 ],才能將頁面傳回正常檢視,並確認標籤中的文字現在具有您在編輯模式中選取的字型樣式。
備註
介面 IWebEditable 可讓您將自訂 EditorPart 控制項與伺服器控制項產生關聯,例如 WebPart 控制項、使用者控制項或自訂伺服器控制項。 控制項 EditorPart 包含在 EditorZone 控制項中,而這個區域及其編輯控制項可為使用者提供使用者介面 (UI) ,以修改相關聯 WebPart 控制項的屬性、外觀和行為。
介面 IWebEditable 包含兩個公開的成員。 WebBrowsableObject屬性可讓您讓 EditorPart 控制項取得相關聯伺服器控制項的參考。 方法 CreateEditorParts 可用來建立與伺服器控制項相關聯之每個自訂 EditorPart 控制項的實例,並將它們當做集合傳回。
介面 IWebEditable 已在基 WebPart 類上實作,但根據預設,此實作不會將任何自訂 EditorPart 控制項與 WebPart 類別產生關聯。 若要將衍生控制項 WebPart 與自訂 EditorPart 控制項產生關聯,您可以覆寫 CreateEditorParts 方法。
給實施者的注意事項
如果您想要使用不是 WebPart Web 元件應用程式中控制項的伺服器控制項, (也就是說,如果您將這些控制項新增至 WebPartZoneBase 區域) ,而且如果您想要將自訂 EditorPart 控制項與這類伺服器控制項產生關聯,則需要實作 IWebEditable 介面。 WebPart衍生控制項不應該實作 介面,因為基 WebPart 類已經這麼做。
屬性
WebBrowsableObject |
取得 WebPart 控制項、使用者控制項或自訂控制項的參考,這項參考將會由 EditorPart 控制項進行編輯。 |
方法
CreateEditorParts() |
傳回與伺服器控制項相關聯之自訂 EditorPart 控制項的集合,該伺服器控制項會實作 IWebEditable 介面。 |
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應