PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2010/CSASPNETBreadcrumbWithQueryString/DynamicBreadcrumb.aspx.cs

#
C# | 99 lines | 54 code | 8 blank | 37 comment | 9 complexity | b019e6f7cf6178f751fea7120ddaae9b MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. * Module Name: DynamicBreadcrumb.aspx.cs
  3. * Project: CSASPNETBreadcrumbWithQueryString
  4. * Copyright (c) Microsoft Corporation
  5. *
  6. * This page shows that even a page is not in the site map, we still can
  7. * create the breadcrumb dynamically.
  8. *
  9. * This source is subject to the Microsoft Public License.
  10. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  11. * All other rights reserved.
  12. *
  13. \*****************************************************************************/
  14. using System;
  15. using System.Web;
  16. namespace CSASPNETBreadcrumbWithQueryString
  17. {
  18. public partial class DynamicBreadcrumb : System.Web.UI.Page
  19. {
  20. protected void Page_Load(object sender, EventArgs e)
  21. {
  22. if (!IsPostBack)
  23. {
  24. // Handle SiteMapResolve event to dynamically change current SiteMapNode.
  25. SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);
  26. }
  27. }
  28. /// <summary>
  29. /// Occurs when the CurrentNode property is accessed.
  30. /// </summary>
  31. /// <param name="sender">
  32. /// The source of the event, an instance of the SiteMapProvider class.
  33. /// </param>
  34. /// <param name="e">
  35. /// A SiteMapResolveEventArgs that contains the event data.
  36. /// </param>
  37. /// <returns>
  38. /// The SiteMapNode that represents the result of the SiteMapResolveEventHandler operation.
  39. /// </returns>
  40. SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
  41. {
  42. // Only need one execution in one request.
  43. SiteMap.SiteMapResolve -= new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);
  44. // We can dynamically create many SiteMapNodes here.
  45. SiteMapNode childNode = new SiteMapNode(SiteMap.Provider, "2");
  46. childNode.Url = "/child.aspx";
  47. childNode.Title = "child";
  48. childNode.ParentNode = new SiteMapNode(SiteMap.Provider, "1");
  49. childNode.ParentNode.Url = "/root.aspx";
  50. childNode.ParentNode.Title = "root";
  51. // Also we can associate the dynamic nodes with the existent site map.
  52. SiteMapNode nodeFromSiteMap = GetSiteMapNode("item");
  53. if (nodeFromSiteMap != null)
  54. {
  55. childNode.ParentNode.ParentNode = nodeFromSiteMap;
  56. }
  57. // Use the new SiteMapNode in the breadcrumb.
  58. return childNode;
  59. }
  60. /// <summary>
  61. /// Get a siteMapNode from the site map.
  62. /// </summary>
  63. /// <param name="key">
  64. /// The resourceKey of the siteMapNode.
  65. /// </param>
  66. /// <returns></returns>
  67. SiteMapNode GetSiteMapNode(string key)
  68. {
  69. return GetSiteMapNode(SiteMap.RootNode, key);
  70. }
  71. SiteMapNode GetSiteMapNode(SiteMapNode rootNode, string key)
  72. {
  73. if (rootNode.ResourceKey == key)
  74. {
  75. return rootNode;
  76. }
  77. else if (rootNode.HasChildNodes)
  78. {
  79. foreach (SiteMapNode childNode in rootNode.ChildNodes)
  80. {
  81. SiteMapNode resultNode = GetSiteMapNode(childNode, key);
  82. if (resultNode != null)
  83. {
  84. return resultNode;
  85. }
  86. }
  87. }
  88. return null;
  89. }
  90. }
  91. }