<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="WEBSWAPP.Website.Data" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Linq" %>
<script runat="server">
private List<NavigationalLink> links = null;
protected void Page_Load(object sender, EventArgs e)
{
//The navigational list of my website is stored in the application cache
links = Cache["WebsiteLinks"] as List<NavigationalLink>;
//my website is organized into categories-chapters-essays
lvCategories.DataSource = links.Where(l => l.LinkType == LinkTypes.Category)
.OrderBy(l => l.Position)
.ToList();
lvCategories.DataBind();
}
protected void lvCategories_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
HiddenField hfId = e.Item.FindControl("hfId") as HiddenField;
if (hfId != null)
{
string categoryId = hfId.Value;
ListView lvChapters = e.Item.FindControl("lvChapters") as ListView;
if (lvChapters != null)
{
lvChapters.DataSource = links
.Where(s => s.LinkType == LinkTypes.Chapter && s.ParentNavigationalLinkId == categoryId)
.OrderBy(l => l.Position)
.ToList();
lvChapters.DataBind();
}
}
}
}
protected void lvChapters_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
HiddenField hfId = e.Item.FindControl("hfId") as HiddenField;
if (hfId != null)
{
string chapterId = hfId.Value;
ListView lvEssays = e.Item.FindControl("lvEssays") as ListView;
if (lvEssays != null)
{
lvEssays.DataSource = links
.Where(s => s.LinkType == LinkTypes.Essay && s.ParentNavigationalLinkId == chapterId)
.OrderBy(l => l.Position)
.ToList();
lvEssays.DataBind();
}
}
}
}
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
Displaying my website's TOC using nested ListView controls</h2>
<p>
In this demo I use 3 nested ListView controls to display the site's hierarchy. You
will find a link to the source code at the bottom.</p>
<%-- First level of the hierarchy of my website's TOC is the site category --%>
<asp:ListView ID="lvCategories" runat="server" ItemPlaceholderID="itemPlaceHolder1"
DataKeyNames="Id" OnItemDataBound="lvCategories_ItemDataBound">
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="itemPlaceHolder1" runat="server"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<%-- this hidden field would help me determine the primary key of the category during the ItemDataBound event handling --%>
<asp:HiddenField ID="hfId" runat="server" Value='<%#Eval("Id") %>' />
<li>Category
<%#Eval("Position") %>:
<asp:Label runat="server" ID="lblCategory" Text='<%#Eval("Text") %>' ToolTip='<%#Eval("ToolTip") %>'></asp:Label>
<%-- Second level of the hierarchy of my website's TOC are Chapters for each category --%>
<asp:ListView ID="lvChapters" runat="server" ItemPlaceholderID="itemPlaceHolder2"
DataKeyNames="Id" OnItemDataBound="lvChapters_ItemDataBound">
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="itemPlaceHolder2" runat="server"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<ItemTemplate>
<asp:HiddenField ID="hfId" runat="server" Value='<%#Eval("Id") %>' />
<li>Chapter
<%#Eval("Position") %>:
<asp:Label runat="server" ID="lblChapter" Text='<%#Eval("Text") %>' ToolTip='<%#Eval("ToolTip") %>'></asp:Label>
<%-- Third level of the hierarchy of my website's TOC are the Essays (i.e. the individual demos) --%>
<asp:ListView ID="lvEssays" runat="server" ItemPlaceholderID="itemPlaceHolder3" DataKeyNames="Id">
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="itemPlaceHolder3" runat="server"></asp:PlaceHolder>
</ul>
</LayoutTemplate>
<EmptyDataTemplate>
There are no Essays for this Chapter.</EmptyDataTemplate>
<ItemTemplate>
<li>Essay
<%#Eval("Position") %>:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("Url") %>' Text='<%#Eval("Text") %>'
ToolTip='<%#Eval("ToolTip") %>'></asp:HyperLink>
<asp:Label ID="lblEssay" runat='server' Text='<%#Eval("ToolTip") %>' ToolTip='<%#Eval("ToolTip") %>'></asp:Label></li>
</ItemTemplate>
</asp:ListView>
</li>
</ItemTemplate>
</asp:ListView>
</li>
</ItemTemplate>
</asp:ListView>
</asp:Content>