Supporting Templates with ASP.NET User Controls
 
Published: 04 Jun 2006
Unedited - Community Contributed
Abstract
In this article, Scott examines how to work with supporting templates with ASP.NET User Controls.
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 13382/ 29
Article Contents:

Introduction

Republished with Permission - Original Article

ASP.NET User Controls (those files with the .ascx extensions) provide a really easy way to encapsulate functionality within an application, and allow it to be re-used across multiple pages and projects (note: for a tutorial on how to create re-usable user control libraries with the VS 2005 Web Application Project check out the C# one here, and the VB one here).

Most developers who create user-controls know that it is possible to easily expose public properties from them so that you can customize them from a page that is using them.  This allows a page developer to declare properties like so on the user-control tag (as well as access them from the code-behind):

Listing 1

<MyLibrary:MyUserControl id="UC1" someproperty="somevalue" runat="server"/>

What is less well known is that in addition to exposing standard properties from user controls, you can also expose template properties.  This allows a page developer to pass templates to the user control like so:

Listing 2

<MyLibrary:MyUserControl id="UC1" someproperty="somevalue" runat="server">
      <TitleTemplate>
           Some custom content I want rendered in the title...
      </TitleTemplate>
      <ItemTemplate>
           Here is a calendar: <asp:calendar id="cal1" runat="server" />
      </ItemTemplate>
   </MyLibrary:MyUserControl>

This allows you to provide much richer UI customization and further re-use.

Robert Seder has posted a nice blog post here that describes how to-do this.

Update: Kris also pointed me at some MSDN documentation that shows the syntax to support this with VB.  You can read it here.

Hope this helps,

Scott

Resources



User Comments

Title: New Link   
Name: Scott
Date: 2007-10-02 8:06:23 AM
Comment:
Here's the new link after a little bit of googling:

http://wss3.theseders.com/robsederblog/Lists/Posts/Post.aspx?List=4c9e886c%2D4b5c%2D4a3e%2D95a0%2De30b3f4a1e9c&ID=67
Title: Broken Link   
Name: Scott
Date: 2007-10-02 8:04:48 AM
Comment:
The Robert Seder blog link is broken

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-25 4:06:30 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search