Scenario
I am having a website, that has few application pages. Those pages have to be linked via custom breadcrumb control. Let's say that this is project management site, and the pages are: Projects.aspx (the list with all available projects), ProjectDetails.aspx (details for a single project), ProjectExpenses.aspx (the expenses details for a selected project) and ProjectResources.aspx (list with employees that are assigned to the selected project). My breadcrumb should have 3 links only: Home -> Projects -> Project Details, but when user opens expenses or resources links, he/she should be able to navigate back to the current Project Info page by clicking on the breadcrumb link.
Sitemap File
I am going to use a simple sitemap file, located in Layouts folder of my project, where my application pages are.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgL6EkI3LCJgqPkogmGifv-y7SZXrb1a3LN0yRFREmrYyf8Zsy5R4cgnHaGj3HIOrP641n5MO0xvsMFV6aY9rplH4mP7eXKVEIrRfa28CHYxbhm8EMG4-hOwiloCimDvQAkO37ohI3eaKo/s400/img+1+-+project+tree.png)
This sitemap describes a tree of project pages according to their location in the hierarchy:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVh3EaguyM19ouLWrV9iBaB6bNJsxeuhO1gVOYQBS0OpwcMJywfS8mQrcBP8deugAIrsBXwFOOR79VQkYRwP4_-T1nouocPwnnZwPuCBjAOXUd3oYkS2SSjzWFQlj7bSUxA5EHmMMOI0U/s400/img+2+-+sitemap.png)
Pay attention to the nodes describing Project Resources and Project Expenses links. They have 2 additional properties called " hideInMenu". I am going to use these properties later on to make sure that those 2 nodes are not shown in my breadcrumb control.
Breadcrumb Control
For a breadcrumb I will use a custom user control (located in ControlTemplates folder). This control will read the sitemap, and based on the data and current location will generate the links to show. Here, as a sample will use the most-dummy approach - read and display right away. In real-life-project, I would wrap each sitemap node in a custom object, or create and XSLT schema to read and display the content for example.
To read the sitemap, I will use XmlSiteMapProvider. There are many samples on the net how to load sitemap file, so I will not stop on this piece now (besides, this sample code will be provided for download). Once sitemap is ready, I can get what is the current node based on my location:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgXAmrsxGTtr36AMSFdq6oaU7TtJHHBA87dx_b3eKy55-5XsvOasNj_AmeNaXXlia3wvL9y7AgoCyiGJ1wy1nGWgBNaYuHUrjtq6V2i2V3G3HknmRX06zjlDFrbWnQIqo3GA45cNaEPjk0/s400/img+3+-+current+node.png)
Each sitemap node has a property ParentNode, and this is what I will use to construct the menu tree:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjAN_-dALy_LY_tTy3OaTqN47CAlYUQ1UgyPPtr0-7qBK-7_cZVgoHv-vN0Yke6tkRqDnn0cs74ag_VjRL1DnASHCvJbuSy6DuW8PekBlnuwpXBGeGIQM_KYWMmdvmbNWsIwYk5lQ0jPqM/s400/img+4+-+load+menu+tree.png)
To check if node should be shown in menu, I use a helper method that checks its "hideInMenu" property:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiIluGPdWYfpvH4O7iTThS46lfhbiZorGqaAHglxvq7h4Ma58vsfI2mK5Pl2xYsHoKMMFJUFfNBXiUFLt8aenVki12JaR0pkxfKuY-U7_fx8O9nAQToyJCwBa5LN6P6QkJVq_Q5Q0zXg3A/s400/img+5+-+show+node+method.png)
When all nodes are loaded in my dictionary, I have to construct the links for the breadcrumb:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSSi28l2S7NMssKp-t-ohpMBS71EL0u3FcUsXHIzrxnhtomhc6-4oclqEOy7z1nMWcpD3jfjpMUrIo77F6MUxqm0ajl_wRcVehpr6LfkzQPemcmlWEGX22tshSm0qkJZXbNFYklHD8Rp4/s400/img+6+-+construct+the+links.png)
And here is the result:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEinA_YDvi94VNmz4kVXHdy7WWTYlYzRiSV_KOds7dZSB8gE7D0usZ_8JMV0HSHOlpLZAsCJyJD6i-GPU4XFl61ICI-M_Wir_lmqbAjYn9OSyYZUpAIT3DMFX5Wv88SBWhO-dGjZLft7RZ8/s400/img+7+-+breadcrumb.png)
Nothing high-tech, but very clean as solution and easy to use. You can download the sample code from here.
No comments:
Post a Comment