EditableRegions again

S

Sebastian

This example shows simplest table with one editable region. Has anyone an
Idea how to extend the designer class to get more editable regions (cells) in
this table (so more content containers editable at design-time)?

Thanks for examples or advice.

1 using System;
2 using System.ComponentModel;
3 using System.ComponentModel.Design;
4 using System.Drawing;
5 using System.Web.UI;
6 using System.Web.UI.Design;
7 using System.Web.UI.Design.WebControls;
8 using System.Web.UI.WebControls;
9
10 namespace embed
11 {
12 [Designer(typeof(MyPartDesigner)),
13 ToolboxData("<{0}:MyPart runat=\"server\"
width=\"200\"></{0}:MyPart>")]
14 public class MyPart : CompositeControl
15 {
16 private Control myTemplateContainer;
17
18 // Define the templates that represent 2 views on the control
19 private ITemplate _view;
20
21 // Create persistable inner properties
22 // for the two editable views
23 [DefaultValue(null),
24 PersistenceMode(PersistenceMode.InnerProperty),
25 TemplateContainer(typeof(ViewContainerForPart))]
26 public virtual ITemplate MyView
27 {
28 get { return _view; }
29 set { _view = value; }
30 }
31
32 // Create a simple table with a row of two clickable,
33 // readonly headers and a row with a single column, which
34 // is the 'container' to which we'll be adding controls.
35 protected override void CreateChildControls()
36 {
37 // Always start with a clean form
38 Controls.Clear();
39
40 // Create a table using the control's declarative properties
41 Table t = new Table();
42 t.CellSpacing = 1;
43 t.BorderStyle = BorderStyle;
44 t.Width = this.Width;
45 t.Height = this.Height;
46
47 // Create the header row
48 TableRow tr = new TableRow();
49 tr.HorizontalAlign = HorizontalAlign.Center;
50 tr.BackColor = Color.LightBlue;
51
52 // This cell represents our content 'container'
53 TableCell tc = new TableCell();
54 tc.Height = new Unit(20);
55
56 //----------------------------------------------------
57 //----------------------------------------------------
58 if (MyView != null)
59 {
60 myTemplateContainer = new ViewContainerForPart(this);
61 MyView.InstantiateIn(myTemplateContainer);
62 tc.Controls.Add(myTemplateContainer);
63 }
64 //----------------------------------------------------
65 //----------------------------------------------------
66
67 tr.Cells.Add(tc);
68 t.Rows.Add(tr);
69
70 // Add the finished table to the Controls collection
71 Controls.Add(t);
72
73 }
74
75
76 protected override void OnDataBinding(EventArgs e)
77 {
78 EnsureChildControls();
79 base.OnDataBinding(e);
80 }
81 }
82
83
84 public class ViewContainerForPart : Control, INamingContainer
85 {
86 private MyPart parent;
87 public ViewContainerForPart(MyPart parent)
88 {
89 this.parent = parent;
90 }
91 }
92
93 //---------------------------------------------------------
94 // Region-based control designer for the above web control,
95 // derived from CompositeControlDesigner.
96 public class MyPartDesigner : CompositeControlDesigner
97 {
98 private MyPart myControl;
99
100 public override void Initialize(IComponent component)
101 {
102 base.Initialize(component);
103 myControl = (MyPart)component;
104 }
105
106 // Make this control resizeable on the design surface
107 public override bool AllowResize
108 {
109 get
110 {
111 return true;
112 }
113 }
114
115 // Use the base to create child controls, then add region markers
116 protected override void CreateChildControls()
117 {
118 base.CreateChildControls();
119
120 // Get a reference to the table, which is the first child
control
121 Table t = (Table)myControl.Controls[0];
122
123 // Add design time markers for each of the three regions
124 if (t != null)
125 {
126
t.Rows[0].Cells[0].Attributes[DesignerRegion.DesignerRegionAttributeName] =
"0";
127 }
128 }
129
130
131 // Create the regions and design-time markup. Called by the
designer host.
132 public override String
GetDesignTimeHtml(DesignerRegionCollection regions)
133 {
134 // Create an editable region and add it to the regions
135 EditableDesignerRegion editableRegion =
136 new EditableDesignerRegion(this,
137 "Content", false);
138 regions.Add(editableRegion);
139
140 // Use the base class to render the markup
141 return base.GetDesignTimeHtml();
142 }
143
144 // Get the content string for the selected region. Called by
the designer host?
145 public override string
GetEditableDesignerRegionContent(EditableDesignerRegion region)
146 {
147 // Get a reference to the designer host
148 IDesignerHost host =
(IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
149 if (host != null)
150 {
151 ITemplate template = myControl.MyView;
152
153 // Persist the template in the design host
154 if (template != null)
155 return ControlPersister.PersistTemplate(template,
host);
156 }
157
158 return String.Empty;
159 }
160
161 // Create a template from the content string and
162 // put it in the selected view.
163 public override void
SetEditableDesignerRegionContent(EditableDesignerRegion region, string
content)
164 {
165 if (content == null)
166 return;
167
168 // Get a reference to the designer host
169 IDesignerHost host =
(IDesignerHost)Component.Site.GetService(typeof(IDesignerHost));
170 if (host != null)
171 {
172 // Create a template from the content string
173 ITemplate template = ControlParser.ParseTemplate(host,
content);
174
175 if (template != null)
176 {
177 myControl.MyView = template;
178 }
179 }
180 }
181 }
182 }
183
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top