Hi,
I made a class DirSSMP that derived from StaticSiteMapProvider. It read out one of directories from E: drive and put those information in to the SiteMapProvider.
I created a .aspx web page with a TreeView and a SiteMapDataSource. In the PageLoad() of .aspx.cs file, I newed the DirSSMP and set it to the SiteMapDataSource.Provider.
When I run the page, it only show me the root node of the directory. When I checked step by step in debug, I did see that the RootNode was created and it got 21 childNodes.
Does anybody can help me out here? Thanks a lot. I'll copy some of my code under here.
----------------------------------------------------------------------------------------------
.aspx
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1">
</asp:TreeView>
</asp:Content>
-----------------------------------------------------------------------------------------------
.aspx.cs
public partial class Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DirSSMP photoDir = new DirSSMP();
photoDir.Initialize("photoDir", null);
SiteMapDataSource1.Provider = photoDir;
TreeView1.ExpandAll();
}
}
--------------------------------------------------------------------------------------------------
DirSSMP.cs
public override void Initialize(string name, NameValueCollection attributes) {
if (IsInitialized)
return;
base.Initialize(name, attributes);
//Get directory information
directory= new DirectoryInfo("E:\\newsite\\Photos");
initialized = true;
}
///
/// SiteMapProvider and StaticSiteMapProvider methods that this derived class must override.
///
// Clean up any collections or other state that an instance of this may hold.
protected override void Clear() {
lock (this) {
rootNode = null;
base.Clear();
}
}
// Build an in-memory representation from persistent
// storage, and return the root node of the site map.
public override SiteMapNode BuildSiteMap() {
// Since the SiteMap class is static, make sure that it is
// not modified while the site map is built.
lock(this) {
// If there is no initialization, this method is being
// called out of order.
if (! IsInitialized) {
throw new Exception("BuildSiteMap called incorrectly.");
}
// If there is no root node, then there is no site map.
if (null == rootNode) {
// Start with a clean slate
Clear();
int nodeID=0;
rootNode=new SiteMapNode(this, nodeID.ToString(),"E:\\newsite\\Photos","Photos");
makeDirTree(rootNode,ref nodeID);
}
return rootNode;
}
}
private void makeDirTree(SiteMapNode parentNode,ref int nodeID)
{
string sURL,sName;
int n;
DirectoryInfo parentDir = new DirectoryInfo(parentNode.Url);
SiteMapNode childNode;
foreach (DirectoryInfo folder in parentDir.GetDirectories())
{
nodeID++;
sURL = folder.FullName;
n = sURL.LastIndexOf("\\"); //Get the dir or file name
sName = sURL.Substring(n + 1);
childNode=new SiteMapNode(this,nodeID.ToString(),sURL,sName);
AddNode(childNode,parentNode);
makeDirTree(childNode,ref nodeID);
}
}