Persist properties correctly with PersistenceModeAttribute
System.Web.UI.PersistenceModeAttribute specifies how a property
should be persisted by the designer. This attribute is usually necessary when
creating custom Web controls.
This attribute takes one parameter, which is a System.Web.UI.PersistenceMode
enumeration member:
-
Attribute- specifies that the property should
be persisted as an attribute.
Use this value for simple (or primitive) properties.
-
InnerProperty - specifies that property should be
persisted as a nested element. Use this value for complex
(or non-primitive) properties, collections, and templates.
Note that PersistenceModeAttribute and PersistenceMode are two different
types.
Let's look at an example for clarity.
Example
public class SearchControl : Control
{
private TextBox _searchTextBox = new TextBox();
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[PersistenceMode(PersistenceMode.InnerProperty)]
public TextBox SearchTextBox
{
get
{
return _searchTextBox;
}
}
}
The above sample control gets persisted the following way on a Web form:
<asp:SearchControl><SearchTextBox Length="1" BackColor="White"/></asp:SearchControl>
See
DesignerSerializationVisibilityAttribute for an example where the
property is persisted as an attribute.
Back to Tips and Tricks