I've got a SiteMapDataSource control that populates an asp:Menu control in my Master page. On issue I have with this is that everytime a new page is loaded, the menu is recreated, and in this case, the menu nodes and values are pulled from the Database. So my pages have slowed down considerably. Is there any way to cache the menu when the user first logs in and then retrieve it from cache on subsequent pages instead of loading it everytime?
HTML Code
This is the class for the SqlSiteMapProvider
HTML Code
Code:
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false" />
<asp:Menu ID="MainMenu" CssClass="menuspace" runat="server" Orientation="Horizontal"
MaximumDynamicDisplayLevels="2" DataSourceID="SiteMapDataSource1" DynamicEnableDefaultPopOutImage="False"
StaticEnableDefaultPopOutImage="False" DynamicPopOutImageUrl="~/images/arrow_rght.gif"
OnMenuItemDataBound="MainMenu_MenuItemDataBound" ItemWrap="true">
<StaticMenuStyle CssClass="menu" />
<StaticMenuItemStyle CssClass="menuItem" />
<StaticSelectedStyle CssClass="menuSelectedItem" />
<DynamicMenuStyle CssClass="menuPopup" BackColor="#8C9FB4" />
<DynamicMenuItemStyle CssClass="menuPopupItem" Font-Strikeout="False" />
<DynamicHoverStyle CssClass="menuPopupItem" />
<StaticHoverStyle CssClass="menuItemHover" />
</asp:Menu>
This is the class for the SqlSiteMapProvider
Code:
public class SqlSiteMapProvider : StaticSiteMapProvider
{
// Track the root node.
private SiteMapNode rootNode;
// Track the connection string, provider name, and stored procedure name.
//private string connectionString;
//private string providerName;
//private string storedProcedure;
private bool initialized = false;
public virtual bool IsInitialized
{
get { return initialized; }
}
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection attributes)
{
if (!IsInitialized)
{
base.Initialize(name, attributes);
// Retrieve the web.config settings.
//providerName = attributes["providerName"];
//connectionString = attributes["connectionString"];
//storedProcedure = attributes["storedProcedure"];
//if (providerName == null || providerName.Length == 0)
// throw new Exception("The provider name was not found.");
//else if (connectionString == null || connectionString.Length == 0)
// throw new Exception("The connection string was not found.");
//else if (storedProcedure == null || storedProcedure.Length == 0)
// throw new Exception("The stored procedure name was not found.");
initialized = true;
}
}
public override SiteMapNode BuildSiteMap()
{
// Since the class is exposed to multiple pages,
// use locking to make sure that the site map is not rebuilt by more than one
// page at the same time.
lock (this)
{
// Start with a clean slate.
Clear();
// Get the results in a DataSet
DataSet ds = new DataSet();
ds = UserInformation.Instance.GetMenuOptions();
DataTable dtSiteMap = ds.Tables["MenuOptions"];
// Get the root node.
DataRow rowRoot = dtSiteMap.Select("ParentID IS NULL")[0];
rootNode = new SiteMapNode(this,
rowRoot["URL"].ToString(), rowRoot["URL"].ToString(),
rowRoot["Title"].ToString(), rowRoot["Description"].ToString());
string rootID = rowRoot["MenuOptionID"].ToString();
// Fill down the hierarchy.
AddChildren(rootNode, rootID, dtSiteMap);
}
return rootNode;
}
private void AddChildren(SiteMapNode rootNode, string rootID, DataTable dtSiteMap)//, System.Collections.IList iRoles)
{
DataRow[] childRows = dtSiteMap.Select("ParentID = " + rootID);
foreach (DataRow row in childRows)
{
//SiteMapNode childNode = new SiteMapNode(this,
// row["URL"].ToString(), row["URL"].ToString(),
// row["Title"].ToString(), row["Description"].ToString());
////childNode.Roles = iRoles;
//string rowID = row["MenuOptionID"].ToString();
SiteMapNode childNode = new SiteMapNode(this,
row["MenuOptionID"].ToString(), row["URL"].ToString(),
row["Title"].ToString(), row["Description"].ToString());
//childNode.Roles = iRoles;
string rowID = row["MenuOptionID"].ToString();
// Use the SiteMapNode AddNode method to add
// the SiteMapNode to the ChildNodes collection.
AddNode(childNode, rootNode);
//Get around the glitch that the Base Class won't let you store a
//URL inside the URL property.
if (row["Title"].ToString() == "Calendar")
childNode.Url = "[URL unfurl="true"]http://www.tssc.biz"[/URL] + row["URL"].ToString();
// Check for children in this node.
AddChildren(childNode, rowID, dtSiteMap);//, iRoles);
}
}
protected override SiteMapNode GetRootNodeCore()
{
return BuildSiteMap();
}
public override SiteMapNode RootNode
{
get { return BuildSiteMap(); }
}
protected override void Clear()
{
lock (this)
{
rootNode = null;
base.Clear();
}
}
}