I am writing a custom composite WebControl and want to expose styles of child controls it encapsulates to the ASP.NET designer. The code I currently have is similar to the skeleton below (which for simplicity only has one child control).
With the code below, I can see property "ChildPanelStyle" in the designer, but when I try to modify one of the properties (e.g. CssClass) in the designer, it immediately resets itself to its default value. It looks like the designer serialization isn't happening.
What am I doing wrong?
UPDATE
Adding a setter for the style property doesn't help and shouldn't be necessary. I've updated the sample with an additional style property that is managed directly by the custom control, rather than simply being the property of a child control.
The HeaderStyle property is persisted properly by the designer, but the ChildPanelStyle property isn't.
Of course I could manage all my styles like HeaderStyle, and apply them during rendering, but I'm hoping there's a simpler solution whereby child controls can take care of themselves, and I don't need any custom rendering.
public class MyControl : CompositeControl
{
Panel myChildPanel;
protected override void CreateChildControls()
{
myChildPanel = new Panel();
this.Controls.Add(myChildPanel);
}
[
Category("Style"),
Description("Child panel style"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(true),
PersistenceMode(PersistenceMode.InnerProperty),
]
public System.Web.UI.WebControls.Style ChildPanelStyle
{
get
{
EnsureChildControls();
return this.myChildPanel.ControlStyle;
}
}
[
Category("Style"),
Description("Header style"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(true),
PersistenceMode(PersistenceMode.InnerProperty),
]
public System.Web.UI.WebControls.Style HeaderStyle
{
get
{
if (_headerStyle == null)
{
_headerStyle = new Style();
if (IsTrackingViewState)
((IStateManager)_headerStyle).TrackViewState();
}
return _headerStyle;
}
}
private System.Web.UI.WebControls.Style _headerStyle;
// ... overrides for save/load/tracking ViewState omitted
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
EnsureChildControls();
base.Render(writer);
}
}