1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| 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 _view;
// Create persistable inner properties
// for the two editable views
[PersistenceMode(PersistenceMode.InnerProperty), DefaultValue(null)]
public virtual ITemplate View
{
get { return _view; }
set { _view = 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;
tr.HorizontalAlign = HorizontalAlign.Center;
// This cell represents our content 'container'
TableCell tc = new TableCell();
tc.Height = new Unit(20);
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)
{
t.Rows[0].Cells[0].Attributes[DesignerRegion.DesignerRegionAttributeName] = "0";
}
}
// Create the regions and design-time markup. Called by the designer host.
public override String GetDesignTimeHtml(DesignerRegionCollection regions)
{
// Create an editable region and add it to the regions
EditableDesignerRegion editableRegion =
new EditableDesignerRegion(this,
"Content", false);
regions.Add(editableRegion);
// 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.View;
// 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);
if (template != null)
{
myControl.View = template;
}
}
}
}
} |
Partager