EditableDesignerRegion 类

定义

表示关联控件的设计时标记内的可编辑内容区域。

public ref class EditableDesignerRegion : System::Web::UI::Design::DesignerRegion
public class EditableDesignerRegion : System.Web.UI.Design.DesignerRegion
type EditableDesignerRegion = class
    inherit DesignerRegion
Public Class EditableDesignerRegion
Inherits DesignerRegion
继承
EditableDesignerRegion
派生

示例

此示例演示如何创建具有两个可单击区域和具有两个 EditableDesignerRegion 视图或模板的 对象的控件。 编译项目,然后在可视化设计器中打开页面,并切换到设计 (WYSIWYG) 视图。 有两个可单击的视图: View1View2。 单击 “View1 ”,将 CheckBox 控件从页面的下半部分拖动到可单击区域下方的空设计器区域。 单击 “视图2 ”,并将 RadioButton 控件拖动到空的设计器区域。 再次单击“ 视图1 ”,然后重新出现具有 CheckBox 的区域。 单击“ 视图2 ”,然后重新出现区域 RadioButton 。 切换回源视图以查看更改如何在 HTML 标记中保留

注意

项目必须具有对 System.Design.dll 程序集的引用。

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.Design;
using System.Web.UI.Design.WebControls;
using System.Web.UI.WebControls;

namespace Samples.ASPNet.ControlDesigners_CS 
{
    [
        Designer(typeof(MyMultiRegionControlDesigner)), 
        ToolboxData("<{0}:MyMultiRegionControl runat=\"server\" width=\"200\"></{0}:MyMultiRegionControl>")
    ]
    public class MyMultiRegionControl : CompositeControl
    {
        // Define the templates that represent 2 views on the control
        private ITemplate _view1;
        private ITemplate _view2;

        // Create persistable inner properties 
        // for the two editable views
        [PersistenceMode(PersistenceMode.InnerProperty), DefaultValue(null)]
        public virtual ITemplate View1
        {
            get { return _view1; }
            set { _view1 = value; }
        }
        [PersistenceMode(PersistenceMode.InnerProperty), DefaultValue(null)]
        public virtual ITemplate View2
        {
            get { return _view2; }
            set { _view2 = value; }
        }

        // The current view on the control; 0 = view1, 1 = view2
        private int _currentView = 0;
        public int CurrentView
        {
            get { return _currentView; }
            set { _currentView = value; }
        }

        // Create a simple table with a row of two clickable, 
        // readonly headers and a row with a single column, which 
        // is the 'container' to which we'll be adding controls.
        protected override void CreateChildControls()
        {
            // Always start with a clean form
            Controls.Clear();

            // Create a table using the control's declarative properties
            Table t = new Table();
            t.CellSpacing = 1;
            t.BorderStyle = BorderStyle;
            t.Width = this.Width;
            t.Height = this.Height;

            // Create the header row
            TableRow tr = new TableRow();
            tr.HorizontalAlign = HorizontalAlign.Center;
            tr.BackColor = Color.LightBlue;

            // Create the first cell in the header row
            TableCell tc = new TableCell();
            tc.Text = "View 1";
            tc.Width = new Unit("50%");
            tr.Cells.Add(tc);

            // Create the second cell in the header row
            tc = new TableCell();
            tc.Text = "View 2";
            tc.Width = new Unit("50%");
            tr.Cells.Add(tc);

            t.Rows.Add(tr);

            // Create the second row for content
            tr = new TableRow();
            tr.HorizontalAlign = HorizontalAlign.Center;

            // This cell represents our content 'container'
            tc = new TableCell();
            tc.ColumnSpan = 2;

            // Add the current view content to the cell
            // at run-time
            if (!DesignMode)
            {
                Control containerControl = new Control();
                switch (CurrentView)
                {
                    case 0:
                        if (View1 != null)
                            View1.InstantiateIn(containerControl);
                        break;
                    case 1:
                        if (View2 != null)
                            View2.InstantiateIn(containerControl);
                        break;
                }

                tc.Controls.Add(containerControl);
            }

            tr.Cells.Add(tc);

            t.Rows.Add(tr);

            // Add the finished table to the Controls collection
            Controls.Add(t);
        }
    }

    //---------------------------------------------------------
    // Region-based control designer for the above web control, 
    // derived from CompositeControlDesigner.
    public class MyMultiRegionControlDesigner : CompositeControlDesigner 
    {
        private MyMultiRegionControl myControl;

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            myControl = (MyMultiRegionControl)component;
        }

        // Make this control resizeable on the design surface
        public override bool AllowResize
        {
            get
            {
                return true;
            }
        }

        // Use the base to create child controls, then add region markers
        protected override void CreateChildControls() {
            base.CreateChildControls();

            // Get a reference to the table, which is the first child control
            Table t = (Table)myControl.Controls[0];

            // Add design time markers for each of the three regions
            if (t != null)
            {
                // View1
                t.Rows[0].Cells[0].Attributes[DesignerRegion.DesignerRegionAttributeName] = "0";
                // View2
                t.Rows[0].Cells[1].Attributes[DesignerRegion.DesignerRegionAttributeName] = "1";
                // Editable region
                t.Rows[1].Cells[0].Attributes[DesignerRegion.DesignerRegionAttributeName] = "2";
            }
        }

        // Handler for the Click event, which provides the region in the arguments.
        protected override void OnClick(DesignerRegionMouseEventArgs e)
        {
            if (e.Region == null)
                return;

            // If the clicked region is not a header, return
            if (e.Region.Name.IndexOf("Header") != 0)
                return;

            // Switch the current view if required
            if (e.Region.Name.Substring(6, 1) != myControl.CurrentView.ToString())
            {
                myControl.CurrentView = int.Parse(e.Region.Name.Substring(6, 1));
                base.UpdateDesignTimeHtml();
            }
        }

        // Create the regions and design-time markup. Called by the designer host.
        public override String GetDesignTimeHtml(DesignerRegionCollection regions) {
            // Create 3 regions: 2 clickable headers and an editable row
            regions.Add(new DesignerRegion(this, "Header0"));
            regions.Add(new DesignerRegion(this, "Header1"));

            // Create an editable region and add it to the regions
            EditableDesignerRegion editableRegion = 
                new EditableDesignerRegion(this, 
                    "Content" + myControl.CurrentView, false);
            regions.Add(editableRegion);

            // Set the highlight for the selected region
            regions[myControl.CurrentView].Highlight = true;

            // Use the base class to render the markup
            return base.GetDesignTimeHtml();
        }

        // Get the content string for the selected region. Called by the designer host?
        public override string GetEditableDesignerRegionContent(EditableDesignerRegion region) 
        {
            // Get a reference to the designer host
            IDesignerHost host = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
            if (host != null)
            {
                ITemplate template = myControl.View1;
                if (region.Name == "Content1")
                    template = myControl.View2;

                // Persist the template in the design host
                if (template != null)
                    return ControlPersister.PersistTemplate(template, host);
            }

            return String.Empty;
        }

        // Create a template from the content string and  
        // put it in the selected view.
        public override void SetEditableDesignerRegionContent(EditableDesignerRegion region, string content)
        {
            if (content == null)
                return;

            // Get a reference to the designer host
            IDesignerHost host = (IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
            if (host != null)
            {
                // Create a template from the content string
                ITemplate template = ControlParser.ParseTemplate(host, content);

                // Determine which region should get the template
                // Either 'Content0' or 'Content1'
                if (region.Name.EndsWith("0"))
                    myControl.View1 = template;
                else if (region.Name.EndsWith("1"))
                    myControl.View2 = template;
            }
        }
    }
}
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.Design.WebControls
Imports System.Web.UI.WebControls

Namespace Samples.ASPNet.ControlDesigners_VB

    < _
        Designer(GetType(MyMultiRegionControlDesigner)), _
        ToolboxData("<{0}:MyMultiRegionControl runat=""server"" width=""200""></{0}:MyMultiRegionControl>") _
    > _
    Public Class MyMultiRegionControl
        Inherits CompositeControl

        ' Define the templates that represent 2 views on the control
        Private _view1 As ITemplate
        Private _view2 As ITemplate
        ' The current view on the control; 0 = view1, 1 = view2
        Private _currentView As Integer = 0

        ' Create persistable inner properties
        <PersistenceMode(PersistenceMode.InnerProperty), DefaultValue(CType(Nothing, ITemplate))> _
        Public Overridable Property View1() As ITemplate
            Get
                Return _view1
            End Get
            Set(ByVal value As ITemplate)
                _view1 = value
            End Set
        End Property

        <PersistenceMode(PersistenceMode.InnerProperty), DefaultValue(CType(Nothing, ITemplate))> _
        Public Overridable Property View2() As ITemplate
            Get
                Return _view2
            End Get
            Set(ByVal value As ITemplate)
                _view2 = value
            End Set
        End Property

        Public Property CurrentView() As Integer
            Get
                Return _currentView
            End Get
            Set(ByVal value As Integer)
                _currentView = value
            End Set
        End Property

        ' Create a simple table with a row of two clickable, 
        ' readonly headers and a row with a single column, which 
        ' is the 'container' to which we'll be adding controls.
        Protected Overrides Sub CreateChildControls()
            ' Start with a clean form
            Controls.Clear()

            ' Create a table
            Dim t As New Table()
            t.CellSpacing = 1
            t.BorderStyle = BorderStyle
            t.Width = Me.Width
            t.Height = Me.Height

            ' Create the header row
            Dim tr As New TableRow()
            tr.HorizontalAlign = HorizontalAlign.Center
            tr.BackColor = Color.LightBlue

            ' Create the first cell in the header row
            Dim tc As New TableCell()
            tc.Text = "View 1"
            tc.Width = New Unit("50%")
            tr.Cells.Add(tc)

            ' Create the second cell in the header row
            tc = New TableCell()
            tc.Text = "View 2"
            tc.Width = New Unit("50%")
            tr.Cells.Add(tc)

            t.Rows.Add(tr)

            ' Create the second row for content
            tr = New TableRow()
            tr.HorizontalAlign = HorizontalAlign.Center

            ' This cell represents our content 'container'
            tc = New TableCell()
            tc.ColumnSpan = 2

            ' Add the current view content to the cell 
            ' at run-time
            If Not DesignMode Then
                Dim containerControl As New Control()
                Select Case CurrentView
                    Case 0
                        If Not (View1 Is Nothing) Then
                            View1.InstantiateIn(containerControl)
                        End If
                    Case 1
                        If Not (View2 Is Nothing) Then
                            View2.InstantiateIn(containerControl)
                        End If
                End Select

                tc.Controls.Add(containerControl)
            End If

            tr.Cells.Add(tc)

            t.Rows.Add(tr)

            ' Add the finished table to the Controls collection
            Controls.Add(t)
        End Sub

    End Class

    ' Region-based control designer for the above web control. 
    ' This is derived from CompositeControlDesigner.
    Public Class MyMultiRegionControlDesigner
        Inherits CompositeControlDesigner

        Private myControl As MyMultiRegionControl

        Public Overrides Sub Initialize(ByVal component As IComponent)
            MyBase.Initialize(component)
            myControl = CType(component, MyMultiRegionControl)
        End Sub

        ' Make this control resizeable on the design surface
        Public Overrides ReadOnly Property AllowResize() As Boolean
            Get
                Return True
            End Get
        End Property

        ' Use the base to create child controls, then add region markers
        Protected Overrides Sub CreateChildControls()
            MyBase.CreateChildControls()

            ' Get a reference to the table, which is the first child control
            Dim t As Table
            t = CType(myControl.Controls(0), Table)

            ' Add design time markers for each of the three regions
            If Not IsNothing(t) Then
                ' View1
                t.Rows(0).Cells(0).Attributes(DesignerRegion.DesignerRegionAttributeName) = "0"
                ' View2
                t.Rows(0).Cells(1).Attributes(DesignerRegion.DesignerRegionAttributeName) = "1"
                ' Editable region
                t.Rows(1).Cells(0).Attributes(DesignerRegion.DesignerRegionAttributeName) = "2"
            End If
        End Sub

        ' Handler for the Click event, which provides the region in the arguments.
        Protected Overrides Sub OnClick(ByVal e As DesignerRegionMouseEventArgs)
            If IsNothing(e.Region) Then
                Return
            End If

            ' If the clicked region is not a header, return
            If e.Region.Name.IndexOf("Header") <> 0 Then
                Return
            End If

            ' Switch the current view if required
            If e.Region.Name.Substring(6, 1) <> myControl.CurrentView.ToString() Then
                myControl.CurrentView = Integer.Parse(e.Region.Name.Substring(6, 1))
                MyBase.UpdateDesignTimeHtml()
            End If
        End Sub

        ' Create the regions and design-time markup. Called by the designer host.
        Public Overrides Function GetDesignTimeHtml(ByVal regions As DesignerRegionCollection) As String
            ' Create 3 regions: 2 clickable headers and an editable row
            regions.Add(New DesignerRegion(Me, "Header0"))
            regions.Add(New DesignerRegion(Me, "Header1"))

            ' Create an editable region and add it to the regions
            Dim editableRegion As EditableDesignerRegion = _
                New EditableDesignerRegion(Me, _
                    "Content" & myControl.CurrentView, False)
            regions.Add(editableRegion)

            ' Set the highlight for the selected region
            regions(myControl.CurrentView).Highlight = True

            ' Use the base class to render the markup
            Return MyBase.GetDesignTimeHtml()
        End Function

        ' Get the content string for the selected region. Called by the designer host?
        Public Overrides Function GetEditableDesignerRegionContent(ByVal region As EditableDesignerRegion) As String
            ' Get a reference to the designer host
            Dim host As IDesignerHost = CType(Component.Site.GetService(GetType(IDesignerHost)), IDesignerHost)

            If Not IsNothing(host) Then
                Dim template As ITemplate = myControl.View1
                If region.Name = "Content1" Then
                    template = myControl.View2
                End If

                ' Persist the template in the design host
                If Not IsNothing(template) Then
                    Return ControlPersister.PersistTemplate(template, host)
                End If
            End If

            Return String.Empty
        End Function

        ' Create a template from the content string and put it 
        ' in the selected view. Called by the designer host?
        Public Overrides Sub SetEditableDesignerRegionContent(ByVal region As EditableDesignerRegion, ByVal content As String)
            If IsNothing(content) Then
                Return
            End If

            ' Get a reference to the designer host
            Dim host As IDesignerHost = CType(Component.Site.GetService(GetType(IDesignerHost)), IDesignerHost)
            If Not IsNothing(host) Then
                ' Create a template from the content string
                Dim template As ITemplate = ControlParser.ParseTemplate(host, content)

                ' Determine which region should get the template
                If region.Name.EndsWith("0") Then
                    myControl.View1 = template
                ElseIf region.Name.EndsWith("1") Then
                    myControl.View2 = template
                End If

            End If
        End Sub
    End Class

End Namespace
<%@ Page Language="C#"  %>
<%@ Register TagPrefix="aspSample" 
    Assembly="Samples.ASPNet.ControlDesigners_CS" 
    Namespace="Samples.ASPNet.ControlDesigners_CS" %>

<!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>Designers Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <aspSample:MyMultiRegionControl ID="myCtl" Runat=Server Width=200 Height=75 BorderStyle=solid >
        </aspSample:MyMultiRegionControl><br />
        <asp:CheckBox ID="CheckBox1" runat="server" />
        <asp:RadioButton ID="RadioButton1" runat="server" />

    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Register TagPrefix="aspSample" 
    Assembly="Samples.ASPNet.ControlDesigners_VB" 
    Namespace="Samples.ASPNet.ControlDesigners_VB" %>

<!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>Designers Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <aspSample:MyMultiRegionControl ID="myCtl" Runat=Server Width=200 Height=75 BorderStyle=solid >
        </aspSample:MyMultiRegionControl><br />
        <asp:CheckBox ID="CheckBox1" runat="server" />
        <asp:RadioButton ID="RadioButton1" runat="server" />

    </div>
    </form>
</body>
</html>

注解

使用 EditableDesignerRegion 类来帮助在设计时管理模板。 ControlDesigner将使用此类的实例及其 GetEditableDesignerRegionContent 方法生成区域内容的 HTML 标记。

构造函数

EditableDesignerRegion(ControlDesigner, String)

使用给定的所有者和名称初始化 EditableDesignerRegion 类的新实例。

EditableDesignerRegion(ControlDesigner, String, Boolean)

使用给定的所有者、名称和 EditableDesignerRegion 属性的初始值创建 ServerControlsOnly 类的新实例。

属性

Content

获取或设置区域内容的 HTML 标记。

Description

获取或设置设计器区域的说明。

(继承自 DesignerRegion)
Designer

获取关联的设计器组件。

(继承自 DesignerObject)
DisplayName

获取或设置设计器区域的友好显示名称。

(继承自 DesignerRegion)
EnsureSize

获取或设置一个值,指示设计宿主是否在设计器区域上显式设置区域大小。

(继承自 DesignerRegion)
Highlight

获取或设置一个值,指示是否在设计图面上突出显示设计器区域。

(继承自 DesignerRegion)
Name

获取对象的名称。

(继承自 DesignerObject)
Properties

获取对象的属性。

(继承自 DesignerObject)
Selectable

获取或设置一个值,指示用户是否可以在设计图面上选择该设计器区域。

(继承自 DesignerRegion)
Selected

获取或设置一个值,指示设计图面上的设计器区域当前是否被选中。

(继承自 DesignerRegion)
ServerControlsOnly

获取或设置一个值,该值指示区域是否只能接受 Web 服务器控件。

SupportsDataBinding

获取或设置一个值,该值指示区域是否可绑定到数据源。

UserData

获取或设置可选用户数据,以关联设计器区域。

(继承自 DesignerRegion)

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetBounds()

检索设计图面上设计器区域的大小。

(继承自 DesignerRegion)
GetChildViewRendering(Control)

返回一个 ViewRendering 对象,该对象包含给定控件的设计时 HTML 标记。

GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetService(Type)

从设计宿主中获取由所提供的类型标识的服务。

(继承自 DesignerObject)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

IServiceProvider.GetService(Type)

有关此成员的说明,请参见 GetService(Type)

(继承自 DesignerObject)

扩展方法

GetKeyedService<T>(IServiceProvider, Object)

IServiceProvider获取 类型的T服务。

GetKeyedServices(IServiceProvider, Type, Object)

IServiceProvider获取 类型的serviceType服务的枚举。

GetKeyedServices<T>(IServiceProvider, Object)

IServiceProvider获取 类型的T服务的枚举。

GetRequiredKeyedService(IServiceProvider, Type, Object)

IServiceProvider获取 类型的serviceType服务。

GetRequiredKeyedService<T>(IServiceProvider, Object)

IServiceProvider获取 类型的T服务。

CreateAsyncScope(IServiceProvider)

新建可用于解析作用域内服务的 AsyncServiceScope

CreateScope(IServiceProvider)

新建可用于解析作用域内服务的 IServiceScope

GetRequiredService(IServiceProvider, Type)

IServiceProvider 获取类型 serviceType 的服务。

GetRequiredService<T>(IServiceProvider)

IServiceProvider 获取类型 T 的服务。

GetService<T>(IServiceProvider)

IServiceProvider 获取类型 T 的服务。

GetServices(IServiceProvider, Type)

IServiceProvider 获取 serviceType 类型服务的枚举。

GetServices<T>(IServiceProvider)

IServiceProvider 获取 T 类型服务的枚举。

GetFakeLogCollector(IServiceProvider)

获取对象,该对象收集发送到假记录器中的日志记录。

GetFakeRedactionCollector(IServiceProvider)

从依赖项注入容器中获取假的重设函数收集器实例。

适用于

另请参阅