UserControls nested tags
I want to encapsulate a ListView control into a simple User Control for easier reuse. Because the layout of the template is always the same (just line count and title headings differ) I thought I could generate it based on the following information:
<asp:SimpleList ID="lstPersons" runat="server"
DataSourceID="dsPersons" DataKey="PersonID" EnablePaging="true" Pager="Numeric">
<Columns>
<Column Title="ID" DataField="PersonID" EnableSorting="true" SelectField="true" Width="30"/>
<Column Title="First Name" DataField="FirstName" EnableSorting="true" Width="150"/>
<Column Title="Last Name" DataField="LastName" EnableSorting="true" Width="150"/>
<Column Title="Salary" DataField="Salary" EnableSorting="true" CssClass="numericValueCell" Width="50"/>
</Columns>
</asp:SimpleList>
When you define public properties in the UserControl class they get available in the markup. But what if you have some kind of List as property. To assign this property in the markup you have to use nested tags within the UserControl definition (like in the example above). How can I do that? I thought that this would be an easy task, but I can't put the pieces together.
This would be the code-behind code for the UserControl without any Attributes (just the properties so far).
namespace UserControls
{
public partial class SimpleList : System.Web.UI.UserControl
{
private ColumnCollection columns;
public ColumnCollection Columns
{
get
{
if (this.columns == null)
{
开发者_开发知识库 this.columns = new MyListItemCollection();
}
return this.columns;
}
}
// Properties
}
public class ColumnCollection : List<Column>
{
}
public class Column
{
// Properties
}
}
I was told that I just have to use the Attributes PersistenceModeAttribute and ParseChildrenAttribute, but how (I really tried)? I hope that dynamically generating the LayoutTemplate and ItemTemplate and binding it to a DataSource control is a little bit more straight forward.
Thanks for your time.
精彩评论