in

ASP.NET Weblogs

-[Danny Chen]- Blog of an ASP.NET QA tester

Tips and info about Site Navigation, ImageMap, Menu and other cool ASP.NET v2.0 features.

Code Listing for 1/27/2006

TemplateLibrary.cs
 
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

namespace MSSamples
{
    
/// <summary>
    /// TemplateLibrary - custom control that holds a list of templates
    ///   This control provides a way to store an arbitrary list of templates
    /// provided declaratively to it through mark-up.  The templates are
    /// made accessible through a property to any other object that needs them
    /// </summary>
    
    [
ParseChildren(true,"Templates"), PersistChildren(false)]
    
public class TemplateLibrary : Control
    {
        
public TemplateLibrary()
        {
            _templates =
new TemplateList();
        }
        
        
private TemplateList _templates;
        [
PersistenceMode(PersistenceMode.InnerProperty)]
        
public TemplateList Templates
        {
            
get
            {
                
return _templates;
            }
        }

    }

    
// The List<TemplateItems> so that a string indexer
    // can also be used to reference items.  (This isn't
    // the most efficient running algorithm, but I don't
    // expect a significantly large number of items to
    // end up in the list and it is memory efficient.)
    public class TemplateList : List<TemplateItem>
    {
        
public TemplateItem this[string key]
        {
            
get
            {
                
foreach (TemplateItem ti in this)
                {
                    
if (string.Compare(key, ti.Name, StringComparison.InvariantCultureIgnoreCase) == 0)
                        
return ti;
                }
                
return null;
            }
        }
    }

    
// Template object - contains a template and a name
    public class TemplateItem : ITemplate
    {
        
private string _name;
        
private ITemplate _template;

        
public string Name
        {
            
get { return _name; }
            
set { _name = value; }
        }

        
// The Template
        // Since it isn't "known" what the exact type of container
        // will be used to instantiate a template, I just assume all
        // of them will implement the IDataItemContainer interface,
        // which is a good idea anyway for containers.
        [PersistenceMode(PersistenceMode.InnerProperty),
        
TemplateContainer(typeof(IDataItemContainer))]        
        
public ITemplate Template
        {
            
get { return _template; }
            
set { _template = value; }
        }

        
// This is just to help reduce one level of dereferencing
        // needed to get a handle on the template object.
        #region ITemplate Members
        
public void InstantiateIn(Control container)
        {
            Template.InstantiateIn(container);
        }
        #endregion
    }
}
Published Jan 27 2006, 10:35 AM by dannychen
Filed under:

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add