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


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">
           Some custom content I want rendered in the title...
           Here is a calendar: <asp:calendar id="cal1" runat="server" />

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,


View Entire Article

User Comments

Title: New Link   
Name: Scott
Date: 2007-10-02 8:06:23 AM
Here's the new link after a little bit of googling:
Title: Broken Link   
Name: Scott
Date: 2007-10-02 8:04:48 AM
The Robert Seder blog link is broken

Product Spotlight
Product Spotlight 

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

©Copyright 1998-2024  |  Page Processed at 2024-05-24 9:51:22 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search