This documentation is archived and is not being maintained.

PersistChildrenAttribute Class

Defines an attribute that is used by ASP.NET server controls to indicate at design time whether nested content that is contained within a server control corresponds to controls or to properties of the server control. This class cannot be inherited.


Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public sealed class PersistChildrenAttribute : Attribute

The PersistChildrenAttribute type exposes the following members.

Public methodPersistChildrenAttribute(Boolean)Initializes a new instance of the PersistChildrenAttribute class using a Boolean value indicating whether to persist nested content as nested controls.
Public methodPersistChildrenAttribute(Boolean, Boolean)Initializes a new instance of the PersistChildrenAttribute class using two Boolean values. One indicating whether to persist nested content as nested controls and the other indicating whether to use a custom persistence method.

Public propertyPersistGets a value that indicates whether the nested content is persisted as nested controls at design time.
Public propertyTypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Public propertyUsesCustomPersistenceGets a value indicating whether the server control provides custom persistence of nested controls at design time.

Public methodEqualsDetermines whether the specified object is equal to the current object. (Overrides Attribute.Equals(Object).)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for the PersistChildrenAttribute class. (Overrides Attribute.GetHashCode().)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsDefaultAttributeReturns a value indicating whether the value of the current instance of the PersistChildrenAttribute class is the default value of the derived clss. (Overrides Attribute.IsDefaultAttribute().)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Public fieldStatic memberDefaultIndicates the default attribute state. The Default field is read-only.
Public fieldStatic memberNoIndicates that nested content should not persist as nested controls at design time. This field is read-only.
Public fieldStatic memberYesIndicates that nested content should persist as controls at design time. The Yes field is read-only.

Explicit interface implemetationPrivate method_Attribute.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.InvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)

The PersistChildrenAttribute is used in combination with the ParseChildrenAttribute to determine how nested content of a control is interpreted. If PersistChildrenAttribute is true and ParseChildrenAttribute is false, the nested content contained within an ASP.NET server control is persisted as controls. If PersistChildrenAttribute is false and ParseChildrenAttribute is true, the nested content is persisted as properties of the server control. For more information about using attributes, see Extending Metadata Using Attributes.

The code example in this section contains two parts. The first code example demonstrates how to set the metadata of a custom control so that at design time, its nested content is persisted as properties of the control. The second code example demonstrates how to use classes in an ASP.NET page.

The following code example demonstrates how to apply the PersistChildrenAttribute attribute so that none of a custom server control's nested controls are persisted as nested controls. The custom server control named CollectionPropertyControl has the PersistChildrenAttribute attribute set to false so that the Employee objects that are added are persisted as nested elements.

using System;
using System.Collections;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;

namespace PersistChildrenSamples
   // The child element class.
   public sealed class Employee
      private String name;
      private String title;
      private String alias;

      public Employee():this ("","",""){}

      public Employee (String name, String title, String alias)
      { = name;
         this.title = title;
         this.alias = alias;
      public String Name
            return name;
            name = value;

      public String Title
            return title;
            title = value;

      public String Alias
            return alias;
            alias = value;
   // Use the PersistChildren attribute to set the Persist
   // property to false so that none of this class's
   // child controls will be persisted as controls. They will
   // be persisted only as child elements of this class.
   // If you set the PersistChildren attribute to true, or if you
   // do not include this attribute when you create a control,
   // the child controls will be persisted as controls.   
   public sealed class CollectionPropertyControl : Control
      private String header;
      private ArrayList employees = new ArrayList();

      public String Header
            return header;
            header = value;

      public ArrayList Employees
            return employees;
      // Override the CreateChildControls method to 
      // add child controls to the Employees property when this
      // custom control is requested from a page.
      protected override void CreateChildControls()
         Label label = new Label();
         label.Text = Header;
         label.BackColor = Color.Beige;
         label.ForeColor = Color.Red;
         Controls.Add(new LiteralControl("<BR> <BR>"));

         Table table = new Table();
         TableRow htr = new TableRow();

         TableHeaderCell hcell1 = new TableHeaderCell();    
         hcell1.Text = "Name";

         TableHeaderCell hcell2 = new TableHeaderCell();
         hcell2.Text = "Title";

         TableHeaderCell hcell3 = new TableHeaderCell();
         hcell3.Text = "Alias";

         table.BorderWidth = 2;
         table.BackColor = Color.Beige;
         table.ForeColor = Color.Red;
         foreach (Employee employee in Employees)
            TableRow tr = new TableRow();

            TableCell cell1 = new TableCell();
            cell1.Text = employee.Name;

            TableCell cell2 = new TableCell();
            cell2.Text = employee.Title;

            TableCell cell3 = new TableCell();
            cell3.Text = employee.Alias;



The following code example demonstrates how to use the CollectionPropertyControl and Employee classes in an ASP.NET page.

<%@ Page Language="C#" %>
<%@ Register TagPrefix="AspSample" Assembly="Samples.AspNet.CS.Controls" Namespace="PersistChildrenSamples" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">

  protected void Page_Load(object sender, EventArgs e)

    // Create two new employees and add them to the custom control.
    Employee e1 = new Employee("Employee 1", "Title 1", "Alias 1");
    Employee e2 = new Employee("Employee 2", "Title 2", "Alias 2");

    // Verify attribute values.
    PersistChildrenAttribute p =

    StringBuilder sb = new StringBuilder();
    sb.Append("The Persist property is " + p.Persist.ToString() + "<br />");
    sb.Append("The UseCustomPersistence property is " + p.UsesCustomPersistence.ToString() + "<br />");
    sb.Append("The IsDefault method returns " + p.IsDefaultAttribute().ToString());
    Message.Text = sb.ToString();


<html xmlns="" >
<head runat="server">
    <form id="form1" runat="server">
      <asp:Label ID="Message"
      <AspSample:CollectionPropertyControl id="CollectionPropertyControl1" 

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.