Describing ASP.net Control properties declaratively

| | Comments (2) | TrackBacks (0)
One of the beauties of the ASP.net system is the way you can declaratively describe controls that sit on the page, and also the contents of collections that they contain as properties, as shown as an example snippet below:

<asp:DataGrid ID="ADataGrid" runat="server"> <Columns> <asp:ButtonColumn ButtonType="PushButton" Text="I'm a button!"> <ItemStyle CssClass="ButtonItem" /> </asp:ButtonColumn> </Columns> </asp:DataGrid>

Writing the code to gain page designer support for this is very simple and doesn't actually involve writing any code! The following code demonstrates a basic (as it doesn't actually do anything!) Web Control that provides designer support for describing the contents of one of its properties declaratively:

[ParseChildren(true)]
[PersistChildren(true)]
[ToolboxData("<{0}:CustomControlUno runat=server></{0}:CustomControlUno>")]
public class CustomControlUno : WebControl, INamingContainer
{
    private Control1ChildrenCollection _children;

    [PersistenceMode(PersistenceMode.InnerProperty)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public Control1ChildrenCollection Children
    {
        get
        {
            if (_children == null)
                _children = new Control1ChildrenCollection();
            return _children;
        }
    }
}

public class Control1ChildrenCollection : List<Control1Child>
{
}

public class Control1Child
{
    private int integerProperty;
    private string stringProperty;

    public string StringProperty
    {
        get { return stringProperty; }
        set { stringProperty = value; }
    }

    public int IntegerProperty
    {
        get { return integerProperty; }
        set { integerProperty = value; }
    }
}

The key is the four attributes, two decorating the class and two decorating the property to be exposed through the markup designer. An example of the markup that could then be written is below:

<Abc:CustomControlUno runat="server" ID="Control1">
    <Children>
        <Abc:Control1Child IntegerProperty="1" StringProperty="Item1" />
        <Abc:Control1Child IntegerProperty="2" StringProperty="Item2" />
    </Children>
</Abc:CustomControlUno>

0 TrackBacks

Listed below are links to blogs that reference this entry: Describing ASP.net Control properties declaratively.

TrackBack URL for this entry: http://www.robertwray.co.uk/mt/mt-tb.cgi/132

2 Comments

Matthew said:

Hi, this is a great post that took me a while to find, but I cannot seem to get it working.

I have built the CustomControlUno.ascx with the code-behind above, yet I am having trouble getting the child control to display on a default.aspx page.

On the aspx page, I have registered the CustomControlUno with @Register and later in the form I have the code included above, yet the compiler cannot identify Abc:Control1Child.

The specific error is
Object reference not set to an instance of an object.

on line

< Abc:Control1Child IntegerProperty="1" StringProperty="Item1" />

Is there something obvious I am missing? Thanks so much in advance!

Rob Author Profile Page said:

This code is not actually for ASCX/Web Custom Controls, but rather for controls that have been created solely as .CS files that inherit from CompositeControl. I'm not sure how it would work for an ASCX based control, unfortunately!

Leave a comment

About this Entry

This page contains a single entry by Rob published on February 13, 2008 9:19 AM.

Getting the values from a Datatable column was the previous entry in this blog.

Delusional Ramblings of an Angry Coder is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.23-en