Primary Navigation

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="primarynavigation.ascx.cs" Inherits="Custom_UserControls_primarynavigation" %>
    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server"  ShowStartingNode="false" />
    <asp:Repeater ID="primaryNavLevel1" runat="server" OnItemDataBound="primaryNavLevel1_ItemDataBound" >
        <HeaderTemplate>    <!-- PrimaryNavigation.ascx -->
        <ul class="sf-menu level1"></HeaderTemplate>
        <ItemTemplate>
            <li><a href="<%# Page.ResolveUrl(((SiteMapNode)(Container.DataItem)).Url)  %>"><%# ((SiteMapNode)(Container.DataItem)).Title %></a><asp:Repeater
                ID="primaryNavLevel2" runat="server"  OnItemDataBound="primaryNavLevel2_ItemDataBound">
                <HeaderTemplate>
                <ul class="level2"></HeaderTemplate>
                <ItemTemplate>
                    <li><a href="<%#Page.ResolveUrl(((SiteMapNode)(Container.DataItem)).Url) %>"><%#DataBinder.Eval(Container.DataItem, "title")%></a><asp:Repeater
                        ID="primaryNavLevel3" runat="server" OnItemDataBound="primaryNavLevel3_ItemDataBound" >
                        <HeaderTemplate>
                        <ul class="level3"></HeaderTemplate>
                        <ItemTemplate>
                            <li><a href="<%#Page.ResolveUrl(((SiteMapNode)(Container.DataItem)).Url)%>"><%#DataBinder.Eval(Container.DataItem, "title")%></a><asp:Repeater
                                ID="primaryNavLevel4" runat="server" >
                                <HeaderTemplate>
                                <ul class="level4"></HeaderTemplate>
                                <ItemTemplate>
                                    <li><a href="<%#Page.ResolveUrl(((SiteMapNode)(Container.DataItem)).Url)%>"><%#DataBinder.Eval(Container.DataItem, "title")%></a>
                                    </li></ItemTemplate><FooterTemplate>
                                </ul></FooterTemplate></asp:Repeater>
                            </li></ItemTemplate><FooterTemplate>
                        </ul></FooterTemplate></asp:Repeater>
                    </li></ItemTemplate><FooterTemplate>
                </ul></FooterTemplate></asp:Repeater>
            </li></ItemTemplate><FooterTemplate>
        </ul>
        <!-- End of PrimaryNavigation.ascx --></FooterTemplate></asp:Repeater>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; public partial class Custom_UserControls_primarynavigation : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
primaryNavLevel1.DataSource = SiteMapDataSource1;
primaryNavLevel1.DataBind(); } protected void primaryNavLevel1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
SiteMapNode node = (SiteMapNode)e.Item.DataItem;
if (node.HasChildNodes)
{
Repeater primaryNavLevel2 = (Repeater)e.Item.FindControl("primaryNavLevel2");
primaryNavLevel2.DataSource = node.ChildNodes;
primaryNavLevel2.DataBind();
}
}
} protected void primaryNavLevel2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
SiteMapNode node = (SiteMapNode)e.Item.DataItem;
if (node.HasChildNodes)
{
Repeater primaryNavLevel3 = (Repeater)e.Item.FindControl("primaryNavLevel3");
primaryNavLevel3.DataSource = node.ChildNodes;
primaryNavLevel3.DataBind();
}
}
} protected void primaryNavLevel3_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
SiteMapNode node = (SiteMapNode)e.Item.DataItem;
if (node.HasChildNodes)
{
Repeater primaryNavLevel4 = (Repeater)e.Item.FindControl("primaryNavLevel4");
primaryNavLevel4.DataSource = node.ChildNodes;
primaryNavLevel4.DataBind();
}
}
}
}

Created: Monday 24th May 2010 9:32 AM
Last Modified: Sunday 11th July 2010 6:45 PM LinkedIn link: View Chris Mills' profile on LinkedIn
Valid XHTML 1.0! Valid CSS! Level A conformance icon, W3C-WAI Web Content Accessibility Guidelines 1.0