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