4.2. Creating a Customizable Navigation Bar
Problem
You want to create a navigation bar that lets you add or remove items without changing code so that you can reuse the navigation bar in multiple applications.
Solution
Create an XML document containing the items that will be displayed in the navigation bar, and then create a user control that uses the contents of the XML document to provide the required customization.
To create the user control:
Create a file with a
.ascxextension.Place the
@ Controldirective at the top of the file.Add a
DataListcontrol configured to render a table with anItemTemplatedefining the cells in the table.
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
UserControlclass.(Optional) Establish properties for the control that will provide the ability to programmatically control the basic look of the navigation bar, such as its background color.
To use the user control in an ASP.NET page:
Register the control by using the
@ Registerdirective at the top of the page.Place the tag for the user control in the HTML where you want the control rendered.
The output of a test page demonstrating a typical navigation bar user
control is shown in Figure 4-2. Example 4-7 shows the XML document we created to define
the contents of the navigation bar. Example 4-8
shows the .ascx file for the user control.
Example 4-9 and Example 4-10 show the VB and C# code-behind files for the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access