Describing Control properties declaratively

One of the beauties of the 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:

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

    public Control1ChildrenCollection Children
            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">
        <Abc:Control1Child IntegerProperty="1" StringProperty="Item1" />
        <Abc:Control1Child IntegerProperty="2" StringProperty="Item2" />

No Comments

Add a Comment