Navigation Controls
The treeView and SiteMapPath controls, introduced in ASP.NET v2.0, are fantastic additions to the built-in controls. As with the other controls mentioned in this chapter, they can be programmatically manipulated, but they can also be programmed declaratively.treeView has been around for a long time; it was originally offered in a somewhat less robust form as part of a control package downloadable from Microsoft. Listing 13.4 demonstrates the simple declaration of a TReeView to create the navigation shown in Figure 13.2.
Listing 13.4. treeView declaration
<asp:TreeView ID="MyTree" Runat="Server" ShowLines="true" Font-Names="Verdana, Arial">
<Nodes>
<asp:TreeNode Text="Home" NavigateUrl="~/Default.aspx" Expanded="true">
<asp:TreeNode Text="Page 1" NavigateUrl="~/Page1.aspx" />
<asp:TreeNode Text="Page 2" NavigateUrl="~/Page2.aspx" />
</asp:TreeNode>
</Nodes>
</asp:TreeView>
Figure 13.2. The treeView rendered in the browser

What's the deal with the tilde (~) in the navigation path? Using the tilde references the root of an application. If you have a subfolder in the site acting as an application, the tilde points to that root, not the site root. In Listing 13.4, an application residing in http://www.mysite.com/myapp would resolve the above path to http://www.mysite.com/myapp/Page1.aspx. |
Listing 13.5. Using SiteMapDataSource with treeView and web.sitemap
Code from an .aspx page[View full width]
web.sitemap
<asp:TreeView ID="MyTree" Runat="Server" ShowLines="true" Font-Names="Verdana, Arial"DataSourceID="MapSource" />
<asp:SiteMapDataSource id="MapSource" runat="server" />
The SiteMapPath control creates a breadcrumb trail of links through the site, based on the web.sitemap file. All it requires is declaration in the page like this: <asp:SiteMapPath ID="SiteMapPath1" Runat="server" />. If you were on Page1.aspx described in the web.sitemap file from Listing 13.5, and it contained this control, you'd get the output shown in Figure 13.3. This is another great example of how little work is required on your part with some declarative controls.
<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
<siteMapNode title="Home" url="~/Default.aspx">
<siteMapNode title="Page 1" url="~/Page1.aspx" />
<siteMapNode title="Page 2" url="~/Page2.aspx" />
</siteMapNode>
</siteMap>
Figure 13.3. The SiteMapPath control rendered in the browser
