User controls are one of the greatest things to come along in a long time. For those coming from Classic ASP, you can ‘sort of’ compart them to the old include files, except these are much better and much more powerful. They are separate files, using the ‘.ascx’ file extension.
In this example, two user controls are used – one for the header (testheader.ascx) of the page and one for the footer (testfooter.ascx).
Just copy each section into pages on your system, creating the same hierarchical setup of these three pages, in order to run this example on your system.
Here’s the code for the header (testheader.ascx):
<div align=”center”>
<font Size=”5″><b>Test Header</b> </font><br>
1212 Mockingbird Heights<br>
Chicago, Il 90503<hr>
</div>
Here’s the code for the footer (testfooter.ascx):
<div align=”center”><hr>
<font Size=”2″><i>Test Footer</i><br> Copyright 2002, All Rights Reserved</font>
</div>
Now – here’s the main page, where each user control is inserted:
<%@ Register TagPrefix=”xprs” tagname=”footer” src=”Testfooter.ascx” %>
<%@ Register TagPrefix=”xprs” tagname=”header” src=”testheader.ascx” %>
<html>
<head>
<meta name=”GENERATOR” Content=”ASP Express 2.0″>
<title>Heading of Page Goes here</title>
</head>
<body>
<xprs:header runat=”server”/>
<i><font Color=”#0000FF”>As you can see – the Header is on the top of this page.
Two lines accomplish this – the registration above and the instantiation of the control in the page itself.</font></i> <p>
This is my text for the page. This is my text for the page. This is my text for the page. This is my text for the page.
This is my text for the page. This is my text for the page. This is my text for the page. This is my text for the page.
This is my text for the page. This is my text for the page. This is my text for the page. This is my text for the page. <p>
<i><font Color=”#0000FF”>Now, below, you can also see the Footer for this page.
Again – two lines accomplish this, also.</font></i> <xprs:footer runat=”server”/>
</body>
</html>