You have a header common to all pages in a site and do not want to repeat the code on every page.
Create a user control containing the header code and add the control to each page.
To create the user control:
Create a file with a .ascx extension.
@ Control directive at the top of the file.
Add the HTML you wish to reuse for the header, making sure to avoid using any
<html>, <head>, <body>, or
In the code-behind class for the control, use the .NET language of your choice to:
Create a user control code-behind class that inherits from the
(Optional) Establish properties for the control that will provide the ability to control the basic look of the header programmatically.
To use the user control in an ASP.NET page:
Register the control by using the
@ Register directive at the top of the page.
Place the tag for the user control where you want the control to be rendered.
To demonstrate this solution, we’ve created a user control that houses some typical header HTML, including
<img> tags for a header image and a divider image. We then show how to use the user control in three different ways: with default parameters, with parameters set in the .aspx file, and with parameters set in the code-behind.
The output of a test page that uses the user control is shown in Figure 5-1. (The second divider line appears in green and the third divider line appears in blue when rendered on the screen.) Example ...