Advertisement
Monday, September 06, 2010

.net Tips and Tricks

New .net Books!
 
LATEST NEWS

04.11.2005
BlueVision becomes a Microsoft Certified Partner!

03.30.2005

03.01.2005
Two development books get an update for Visual Studio .NET 2003

01.05.2005
BlueVision launches the Community Technology Preview release of CopAlert™, www.copalert.com

12.20.2002
BlueVision in MSDN Magazine
Tips and Tricks

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

Terms Of Use © 2000 - 2010 BLUEVISION LLC. ALL RIGHTS RESERVED. Privacy Policy