locked
Custom Database-Driven Site Map Provider RRS feed

  • Question

  • User1904059947 posted

    I saw your tutorial with the name Custom Database-Driven Site Map Provider, I'm try to put it in a Custom Menu using a SiteMapDataSource and the Site Map Provider, I want to modify the menu when a user enter the site so I use the next code in the loggin method: 

    1        protected void Login1_LoggedIn(object sender, EventArgs e)
    2        {
    3            SiteMapProviderBLL sitemap = new SiteMapProviderBLL();
    4            UsuariosBLL usuarioslogic = new UsuariosBLL();
    5            sitemap.id_usuario = usuarioslogic.GetIdUserByUserName(Login1.UserName.ToString())[0][0].ToString();
    6            sitemap.BuildSiteMap();
    7        }
    

    The Varible id_usuario I use to bring the specify Menu from the user from a Database, here's the code of the provider:

    1    public class SiteMapProviderBLL : StaticSiteMapProvider
    2    {
    3        private readonly object siteMapLock = new object();
    4        private SiteMapNode root = null;
    5        public const string CacheDependencyKey =
    6            "SiteMapProviderCacheDependency";
    7        public string id_usuario = string.Empty; //For example 93efd296-2d8a-4d60-ad65-5c82122d8rty
    8    
    9        public override SiteMapNode BuildSiteMap()
    10       {
    11           // Use a lock to make this method thread-safe
    12           lock (siteMapLock)
    13           {
    14               // First, see if we already have constructed the
    15               // rootNode. If so, return it...
    16               if (root != null)
    17                   return root;
    18   
    19               // We need to build the site map!
    20   
    21               // Clear out the current site map structure
    22               base.Clear();
    23   
    24               // Get the categories and products information from the database
    25               MENUTableAdapter menuAdapter = new MENUTableAdapter();
    26               DBPrueba.MENUDataTable menuDataTable = menuAdapter.GetData(id_usuario);
    27   
    28               // Create the root SiteMapNode
    29               root = new SiteMapNode(
    30                   this, "root", "~/Default.aspx", "Inicio");
    31               AddNode(root);
    32   
    33               // Create SiteMapNodes for the categories and products
    34               foreach (DBPrueba.MENURow objeto in menuDataTable)
    35               {
    36                   // Add a new category SiteMapNode, if needed
    37                   string categoryKey, categoryName;
    38                   bool createUrlForCategoryNode = true;
    39                   if (objeto.IsNull("OBJETO"))
    40                   {
    41                       categoryKey = "Category:None";
    42                       categoryName = "None";
    43                       createUrlForCategoryNode = false;
    44                   }
    45                   else
    46                   {
    47                       categoryKey = string.Concat("Category:", objeto.OBJETO);
    48                       categoryName = objeto.TEXTO;
    49                   }
    50   
    51                   SiteMapNode categoryNode = FindSiteMapNodeFromKey(categoryKey);
    52   
    53                   // Add the category SiteMapNode if it does not exist
    54                   if (categoryNode == null)
    55                   {
    56                       string productsByCategoryUrl = string.Empty;
    57                       if (createUrlForCategoryNode)
    58                           productsByCategoryUrl =
    59                               "~/" + objeto.OBJETO;
    60   
    61                       categoryNode = new SiteMapNode(
    62                           this, categoryKey, productsByCategoryUrl, categoryName);
    63                       AddNode(categoryNode, root);
    64                   }
    65               }
    66               
    67               // Add a "dummy" item to the cache using a SqlCacheDependency
    68               // on the Products and Categories tables
    69               System.Web.Caching.SqlCacheDependency menuTableDependency =
    70                   new System.Web.Caching.SqlCacheDependency("ssslpBD", "ssslp_Menu");
    71   
    72               // Create an AggregateCacheDependency
    73               System.Web.Caching.AggregateCacheDependency aggregateDependencies =
    74                   new System.Web.Caching.AggregateCacheDependency();
    75               aggregateDependencies.Add(menuTableDependency);
    76   
    77               // Add the item to the cache specifying a callback function
    78               HttpRuntime.Cache.Insert(
    79                   CacheDependencyKey, DateTime.Now, aggregateDependencies,
    80                   Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
    81                   CacheItemPriority.Normal,
    82                   new CacheItemRemovedCallback(OnSiteMapChanged));
    83   
    84   
    85               // Finally, return the root node
    86               return root;
    87           }
    88       }
    
    

     All this works if I put the Id Usuario with a predefine value and when I load my site for first time, but if I use the code of the loggin to change de Id it doesn't work, I'm trying to use a Databind to the control of the SiteMapDataSource.

     Does any one know how I can load the new sitemap for my menu or what method do I need to reload the sitemap??

    Wednesday, August 1, 2007 9:13 AM

Answers

  • User-240370940 posted

    Hi  mr_efe,

    77               // Add the item to the cache specifying a callback function
    78               HttpRuntime.Cache.Insert(
    79                   CacheDependencyKey, DateTime.Now, aggregateDependencies,
    80                   Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
    81                   CacheItemPriority.Normal,
    82                   new CacheItemRemovedCallback(OnSiteMapChanged));

    I'm not sure about this, but it sounds you are "screwing" on the wrong screws. AFAIK is the HttpRuntime object a very long time before the LoginControls initialized. So the whole data-object for youre SiteMapProvider is completely in Cache on not accessable from the later initialized LoginControls.

    
    

    All this works if I put the Id Usuario with a predefine value and when I load my site for first time, but if I use the code of the loggin to change de Id it doesn't work, I'm trying to use a Databind to the control of the SiteMapDataSource.

    this behavior assured me in my mind.

    Does any one know how I can load the new sitemap for my menu or what method do I need to reload the sitemap??

    AFAIK the reload changes nothing, because the same sitemap is allways present to all users on the site.

    Thats only my two cents and can be wrong.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 1, 2007 11:51 AM