Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Dynamic Templates for the Repeater, DataList and DataGrid Controls : Page 4

This article explains why a single template hard-coded at design-time is not always the best option when using DataGrid, DataList and Repeater controls that must generate different outputs according to the logged-in user, a user's choice, or any other condition. It then provides detailed solutions to implement dynamic templates, by writing external user controls, or classes that implement the ITemplate interface.


advertisement

We’re done with the custom template class, and we can use it. We first create a new instance of the class, passing to its constructor method the colors to be used for the different titles of courtesy, and then assign this instance to the ItemTemplate property of a Repeater or DataList, as shown below:

' create a new instance of the custom template class, ' and load it in the 2 template-based controls Dim mct As New MyCustomTemplate(Color.Pink, Color.Cyan, _ Color.LightGreen, Color.Black) Repeater2.ItemTemplate = mct Datalist2.ItemTemplate = mct



For the DataGrid we first retrieve a reference to a TemplateColumn, and then set its ItemTemplate property:

' get a reference to the DataGrid’s template column, and set its ' template to the MyCustomTemplate instance Dim templCol2 As TemplateColumn = CType( _ Datagrid2.Columns(1), TemplateColumn) templCol2.ItemTemplate = mct

The following screenshot shows how the developed custom template class actually works with all these different controls:

Try to change the BindControls method written before so that it uses DataReaders instead of a DataSet as a data source, and you’ll see that the page will continue to display correctly.

In this article I mentioned quite a few situations where you may need custom templates, but you may surely think about others. Templates defined in separate files are great when you need to use different data representation basing on the current user or on other options, while custom template classes are best used when you also have to change the output on a row-by-row basis, and when you want to achieve the maximum code encapsulation and reusability.

Note: You can download the sample code from the link in the left column.



Marco Bellinaso is VB2TheMax's technical editor, and works as a software developer and trainer for Code Architects Srl, an Italian company that specializes in .NET. He's been working with the .NET Framework since the Beta 1, and is now particularly interested in e-commerce design and implementation solutions with SQL Server, ASP.NET, and web services, with both C# and VB.NET. Marco is a co-author for a number of Wrox Press books, among which are ASP.NET Website Programming, Fast Track ASP.NET and Visual C# .NET: a Guide for VB6 Developers. He is also a technical reviewer for other books, a contributing editor for two leading Italian programming magazines: Computer Programming and Visual Basic & .NET Journal (Italian licensee for Visual Studio Magazine), and co-author of VBMaximizer add-in for VB6.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date