locked
Templated databound controls - template property RRS feed

  • Question

  • User-11840556 posted

    I have a templated databound control with several templates. I want to extend this with some new functionality. Ideally I want to do something like this:

    1    <MyControl runat="server">    
    2    <ClassTemplate ClassName="Article">template definition comes here    </TemplateClass
    3    <ClassTemplate ClassName="News">template definition comes here </TemplateClass>    
    4    <ClassTemplate ClassName="BlogPost">template definition comes here     </TemplateClass>   
    5    </MyControl>

    Based on the class of the bound objects, I can select the appropriate template. Is this possible?
    Sunday, May 17, 2009 4:20 AM

All replies

  • User-109060882 posted

    Hi there,

    I'm pretty sure that it's possible to do what you want. Your control would have a property of type List<ClassTemplate>:

    private List<ClassTemplate> _classTemplates = new List<ClassTemplate>();

    public List<ClassTemplate> ClassTemplates {

        get {

            return _classTemplates;

        }

    }

    Make sure to mark this property as being an inner property with the appropriate attribute, as well as any other attributes.

    In the ClassTemplate type you'd have two properties: a get/set string property for the ClassName and another get/set property for the template. The template property should be marked as an InnerDefaultProperty.

    It's then up to your control to have some logic to pick the appropriate template for each item type.

    If any of this doesn't make sense please show how far you've gotten and we can try to fill in the missing parts.

    Thanks,

    Eilon

    Tuesday, June 9, 2009 1:34 PM