Hendrik Swanepoel

templated custom controls in asp.net

Recently, I had to write a templated databound custom control in asp.net.

The reason for this is that we wanted a control which we could reuse throughout the site, and it had toe be very flexible. It needed to have paging capabilities (with completely customized look + feel), searching, sorting and alphabetical filtering.

All of these should also be able to be hidden, based on attributes on the control. Since non of the built-in controls provide this functionality, we had to go the custom control route.

I found a lot of helpful links on the web, but non of them exactly what I wanted. I decided to write a wrapper object to a repeater. To solve the problem in regards of retaining the template functionality of the repeater, I decided to use “proxy properties” on my custom class, effectively passing my custom class' template to my contained repeater.

My custom class had a private variable of type ITemplate and a corresponding property:

private ITemplate itemTemplate = null;

The property has an attribute associated which specifies that the property is a TemplateContainer property of type RepeaterItem.

[
PersistenceMode(PersistenceMode.InnerProperty),
TemplateContainer(typeof(RepeaterItem))
]
public ITemplate ItemTemplate
{
 get
 {
  return itemTemplate;
 }
 set
 {
  itemTemplate = value;
 }
}

Then, when the repeater is initialized, we set the corresponding template property to our properties value:


   if(ItemTemplate != null)
   {
    innerRepeater.ItemTemplate = ItemTemplate;
   }

Here's some links on authoring custom templated data bound controls:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcustomizingcontrolswithtemplates.asp

http://samples.gotdotnet.com/quickstart/aspplus/doc/webdatalist.aspx

http://msdn.microsoft.com/asp.net/using/building/webcontrols/default.aspx?pull=/library/en-us/dnaspp/html/databoundtemplatedcontrols.asp

Posted: Sep 27 2004, 06:54 AM by hendrik | with 4 comment(s)
Filed under:
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: