IWebEditableとは? わかりやすく解説

IWebEditable インターフェイス

メモ : このインターフェイスは、.NET Framework version 2.0 で新しく追加されたものです。

WebPart コントロールに関連付けられているカスタムの編集コントロールを開発者が指定するためのインターフェイスを提供します。

名前空間: System.Web.UI.WebControls.WebParts
アセンブリ: System.Web (system.web.dll 内)
構文構文

public interface IWebEditable
public interface class IWebEditable
public interface IWebEditable
public interface IWebEditable
解説解説

IWebEditable インターフェイスでは、カスタム EditorPart コントロールを、WebPart コントロール、ユーザー コントロール、またはカスタム サーバー コントロールなどのサーバー コントロールに関連付けることができます。EditorPart コントロールは、EditorZone コントロールに格納されており、その編集コントロールを持つこのゾーンでは、関連付けられている WebPart コントロールのプロパティ、外観、および動作を変更するためのユーザー インターフェイス (UI) がエンド ユーザーに提供されます。

IWebEditable インターフェイスには、公開されているメンバが 2 つ含まれています。WebBrowsableObject プロパティを使用して、EditorPart コントロールは、関連付けられているサーバー コントロールへの参照を取得します。CreateEditorParts メソッドは、サーバー コントロールに関連付けられている各カスタム EditorPart コントロールのインスタンスを作成し、それらをコレクションとして返すために使用されます。

IWebEditable インターフェイスは、既に基本 WebPart クラスに実装されています。ただし、既定ではこの実装はカスタム EditorPart コントロールを WebPart クラスに関連付けません。派生 WebPart コントロールをカスタム EditorPart コントロールに関連付けるために、CreateEditorParts メソッドをオーバーライドできます。

実装時の注意 Web パーツ アプリケーションで WebPart コントロールではないサーバー コントロールを使用する場合 (つまり、これらのコントロールを WebPartZoneBase ゾーンに追加する場合)、およびカスタム EditorPart コントロールをそのようなサーバー コントロールに関連付ける場合は、IWebEditable インターフェイスを実装する必要があります。派生 WebPart コントロールの場合は、基本 WebPart クラスが既にインターフェイスを実装しているので、実装は必要ありません。

使用例使用例

カスタム WebPart コントロールで IWebEditable インターフェイスのメソッドをオーバーライドする方法を次のコード例に示します。さらに、WebPart コントロールが編集モードになったときに、カスタム EditorPart コントロールのインスタンスを作成します。

この例は、4 つの部分で構成されます。

コード例の最初の部分は、カスタム TextDisplayWebPart クラスです。このクラスは、WebPart クラスから派生し、IWebEditable インターフェイスを実装して、CreateEditorParts メソッドおよび WebBrowsableObject プロパティの特定の実装を提供します。また、TextDisplayWebPart クラスに入れ子にされているのは、基本 EditorPart クラスから派生するプライベートのカスタム TextDisplayEditorPart クラスです。コード例を実行するためには、このソース コードをコンパイルする必要があります。それを明示的にコンパイルし、コンパイル済みのアセンブリを Web サイトの Bin フォルダまたはグローバル アセンブリ キャッシュに配置できます。サイトの App_Code フォルダにソース コードを配置し、実行時に動的にコンパイルすることもできます。両方のコンパイル方法を示すチュートリアルについては、「チュートリアル : カスタム サーバー コントロールの開発と使用」を参照してください。

Imports System
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
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 (input.Text != String.Empty)
      {
        _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;
        }
      }
    }
  }
}

コード例の 2 番目の部分は、カスタム コントロールをホストする Web ページです。EditorZone コントロールはページのマークアップで宣言されていますが、カスタム EditorPart コントロールは、実行時にプログラムによって追加できるので、ここで参照される必要はありません。

<%@ page language="vb" %>
<%@ register TagPrefix="uc1" 
  TagName="DisplayModeUC" 
  Src="DisplayModeUCvb.ascx" %>
<%@ register tagprefix="aspSample" 
  Namespace="Samples.AspNet.VB.Controls"
 
  Assembly="TextDisplayWebPartVB"
 %>

<html>
  <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="c#" %>
<%@ register TagPrefix="uc1" 
  TagName="DisplayModeUC" 
  Src="DisplayModeUCcs.ascx" %>
<%@ register tagprefix="aspSample" 
  Namespace="Samples.AspNet.CS.Controls" 
  Assembly="TextDisplayWebPartCS" %>

<html>
  <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>

コード例の 3 番目の部分は、ユーザーがページを編集モードに切り替えることができるようにするユーザー コントロールです。ユーザー コントロールは、ホストする Web ページで参照されています。このユーザー コントロールの作成方法の詳細については、「チュートリアル : Web パーツ ページでの表示モードの変更」を参照してください。

<%@ 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>
<%@ 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>

コード例を実行するには、ホストする Web ページをブラウザに読み込み、テキスト ボックスに何らかのテキストを追加し、[Set Label Content] ボタンをクリックして、コントロールのラベルを更新します。ページを編集モードに切り替えるには、表示モードを含むドロップダウン リストの [編集] を選択します。カスタム TextDisplayEditorPart コントロールの UI を表示するには、TextDisplayWebPart コントロールの動詞メニューのドロップダウン矢印をクリックし、[編集] を選択します。編集 UI で、フォント スタイルを含むドロップダウン リストを使用して、TextDisplayWebPart コントロールのラベルのテキスト スタイルを更新します。表示モードのドロップダウン リストの [Browse Mode] をクリックして、ページを標準のビューに戻し、編集モードで選択したフォント スタイルがラベルのテキストに適用されていることを確認する必要があります。

.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

IWebEditable プロパティ


パブリック プロパティパブリック プロパティ

参照参照

関連項目

IWebEditable インターフェイス
System.Web.UI.WebControls.WebParts 名前空間
EditorPart クラス

その他の技術情報

ASP.NET Web パーツ ページ

IWebEditable メソッド


パブリック メソッドパブリック メソッド

参照参照

関連項目

IWebEditable インターフェイス
System.Web.UI.WebControls.WebParts 名前空間
EditorPart クラス

その他の技術情報

ASP.NET Web パーツ ページ

IWebEditable メンバ




英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「IWebEditable」の関連用語

IWebEditableのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



IWebEditableのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS