ユーザーコントロールにコレクションのプロパティを持たせる

MyList.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyList.ascx.cs" Inherits="Sample.MyList" %>

<fieldset class="MyListControl">
    <legend><%= this.Caption %></legend>
    <ul>
<% 
    foreach (var item in this.Items)
    {
%>
        <li>Item: <%= item.Text %></li>
<%
    }
%>
    </ul>
</fieldset>


MyList.ascx.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sample
{
    [ParseChildren(true)]
    [PersistChildren(false)]
    public partial class MyList : System.Web.UI.UserControl
    {
        public string Caption
        {
            get;
            set;
        }

        [MergableProperty(false)]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public List<MyListItem> Items
        {
            get;
            set;
        }
    }

    public class MyListItem
    {
        public string Text
        {
            get;
            set;
        }
    }
}


aspx (1)

<%@ Register tagprefix="my" src="MyList.ascx" tagname="MyList" %>
<%@ Register tagprefix="my" Assembly="Sample" Namespace="Sample" %>


aspx (2)

<my:MyList runat="server" Caption="List 1">
    <Items>
        <my:MyListItem Text="Item 1" />
        <my:MyListItem Text="Item 2" />
        <my:MyListItem Text="Item 3" />
    </Items>
</my:MyList>