Child tags disappearing
-
I'm about to develop some WebControls that should ease the webdesign in a project. But I'm having some problems with composite controls.
-
When I add a child tag for a server control, the control isn't automatically defined in the codebehind page.
-
If I change a property for the main control using the property browser in VS.NET, all the child tags disappears.
I can live with the first problem, but the second is somewhat of a timebomb. Here's the code:
using System; using System.Collections; using System.ComponentModel; using System.ComponentModel.Design; using System.Text; using System.Web.UI; using System.Web.UI.Design; using System.Web.UI.WebControls; namespace MyControls { #region GreyBox /// /// A control for putting the child elements inside the inner cell in a table. /// [ToolboxData("<{0}:GreyBox runat='server'>")] [ParseChildren(true, "BoxChildren")] [DesignerAttribute(typeof(GreyBoxDesigner), typeof(IDesigner))] public class GreyBox : System.Web.UI.WebControls.WebControl, INamingContainer { #region MEMBER VARIABLES /// /// The collection of child controls. /// protected readonly ControlCollection boxChildren; #endregion #region CONSTRUCTORS /// /// Creates a new instance of this control. /// public GreyBox(): base("table") { this.boxChildren = new ControlCollection(this); this.CssClass = "greybox"; } #endregion #region PROPERTIES protected override void AddParsedSubObject(object obj) { if (obj is Control) { boxChildren.Add(obj as Control); } } /// /// The collection of child controls. /// [Bindable(false), Category("Appearance")] public ControlCollection BoxChildren { get { return boxChildren; } } #endregion #region METHODS public static TableRow CreateAndAddTableRow(string cssClass, ControlCollection collection) { TableRow tr = new TableRow(); tr.CssClass = cssClass; collection.Add(tr); return tr; } public static TableCell CreateAndAddTableCell(string cssClass, WebControl ctrl) { TableCell td = new TableCell(); td.CssClass = cssClass; ctrl.Controls.Add(td); return td; } public static void CreateChildControlsAndInsertIntoControl(ControlCollection collection, ControlCollection boxChildren) { TableRow row1 = CreateAndA
-
-
I'm about to develop some WebControls that should ease the webdesign in a project. But I'm having some problems with composite controls.
-
When I add a child tag for a server control, the control isn't automatically defined in the codebehind page.
-
If I change a property for the main control using the property browser in VS.NET, all the child tags disappears.
I can live with the first problem, but the second is somewhat of a timebomb. Here's the code:
using System; using System.Collections; using System.ComponentModel; using System.ComponentModel.Design; using System.Text; using System.Web.UI; using System.Web.UI.Design; using System.Web.UI.WebControls; namespace MyControls { #region GreyBox /// /// A control for putting the child elements inside the inner cell in a table. /// [ToolboxData("<{0}:GreyBox runat='server'>")] [ParseChildren(true, "BoxChildren")] [DesignerAttribute(typeof(GreyBoxDesigner), typeof(IDesigner))] public class GreyBox : System.Web.UI.WebControls.WebControl, INamingContainer { #region MEMBER VARIABLES /// /// The collection of child controls. /// protected readonly ControlCollection boxChildren; #endregion #region CONSTRUCTORS /// /// Creates a new instance of this control. /// public GreyBox(): base("table") { this.boxChildren = new ControlCollection(this); this.CssClass = "greybox"; } #endregion #region PROPERTIES protected override void AddParsedSubObject(object obj) { if (obj is Control) { boxChildren.Add(obj as Control); } } /// /// The collection of child controls. /// [Bindable(false), Category("Appearance")] public ControlCollection BoxChildren { get { return boxChildren; } } #endregion #region METHODS public static TableRow CreateAndAddTableRow(string cssClass, ControlCollection collection) { TableRow tr = new TableRow(); tr.CssClass = cssClass; collection.Add(tr); return tr; } public static TableCell CreateAndAddTableCell(string cssClass, WebControl ctrl) { TableCell td = new TableCell(); td.CssClass = cssClass; ctrl.Controls.Add(td); return td; } public static void CreateChildControlsAndInsertIntoControl(ControlCollection collection, ControlCollection boxChildren) { TableRow row1 = CreateAndA
+ The ToolboxData attribute should be used as below:
[ToolboxData("<{0}:GreyBox runat='server'></{0}:GreyBox>")]
+ Any child element should be added between the opening and closing tags of the BoxChildren element. + In order for child controls not to disappear when setting the parent properties at design time, the BoxChildren property should be serialized and persisted with the DesignerSerializationVisibility and PersistenceMode attributes For more information on these attributes, see MSDN DesignerSerializationVisibilityAttribute Class[^] PersistenceModeAttribute Class[^]
-
-
+ The ToolboxData attribute should be used as below:
[ToolboxData("<{0}:GreyBox runat='server'></{0}:GreyBox>")]
+ Any child element should be added between the opening and closing tags of the BoxChildren element. + In order for child controls not to disappear when setting the parent properties at design time, the BoxChildren property should be serialized and persisted with the DesignerSerializationVisibility and PersistenceMode attributes For more information on these attributes, see MSDN DesignerSerializationVisibilityAttribute Class[^] PersistenceModeAttribute Class[^]
Thanks! :beer: :rose: And GetPersistInnerHtml() in my builder made it work like a charm. "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus
-
Thanks! :beer: :rose: And GetPersistInnerHtml() in my builder made it work like a charm. "After all it's just text at the end of the day. - Colin Davies "For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus