SiteMenu Builder

by Ivan Dimitrov

This post will show you how to create custom navigation menu for your website, where you can specify which pages you want to see and which not. There are many cases where you want to have two level navigation or several navigational  menus. Each time you will have to do some coding over the menu so that it will start showing the appropriate items/nodes.

In this post I am creating a custom class that inherits from WebUITypeEditor.

First I am going to build WebUITypeEditor using RadTreeView control which supports multiple selection. This seems to be the most appropriate control to achieve my goal. I will bind the control to a list containing all pages by using the CmsManager.GetPages() method. Then I will return the ID of each node to my property as a Guid array.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Cms.Web.UI;
using Telerik.Web.UI;
using Telerik.Framework.Web;
using Telerik.Cms;
using System.Collections;
using System.Web.UI;
using Telerik.Cms.Web;
 
/// <summary>
/// Summary description for DynamicMenuBuilderWebUITypeEditor
/// </summary>
public class DynamicMenuBuilderWebUITypeEditor : WebUITypeEditor<Guid[]>
{
    public DynamicMenuBuilderWebUITypeEditor()
    {
 
    }
 
    public override Guid[] Value
    {
        get
        {
            Guid pageId = Guid.Empty;
            List<Guid> selectedIds = new List<Guid>();
            foreach (RadTreeNode node in TreeView.SelectedNodes)
            {
                if (node != null)
                {
                    CmsSiteMapNode smNode = (CmsSiteMapNode)SiteMap.Provider.FindSiteMapNode("~/" + node.FullPath + ".aspx");
                    ICmsPage cmsPage = manager.GetPage(smNode.CmsPage.ID) as ICmsPage;
                    pageId = cmsPage.ID;
                    selectedIds.Add(pageId);
                }
            }
            return selectedIds.ToArray();
        }
        set
        {
            this.ViewState["selectedPage"] = value;
            base.ChildControlsCreated = false;
        }
    }
 
    public string Templte
    {
        get
        {
            object o = this.ViewState["Template"];
            if (o == null)
                return ("~/CustomControls/PageIDWebEditor.ascx");
            return (string)o;
        }
        set
        {
            this.ViewState["Template"] = value;
        }
    }
    
 
    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        this.manager = new CmsManager();
        this.template = ControlUtils.GetTemplate<SelectorTemplate>(this.Templte);
        this.template.InstantiateIn(this);
        var manager = new CmsManager();
        IList allPages = manager.GetPages();
        TreeView.MultipleSelect = true;
        TreeView.DataSource = allPages;
        TreeView.DataTextField = "MenuName";
        TreeView.DataFieldParentID = "ParentID";
        TreeView.DataFieldID = "ID";
        TreeView.DataNavigateUrlField = "StaticUrl";
        TreeView.DataBind();
    }
 
    protected virtual RadTreeView TreeView
    {
        get
        {
            return this.Controls[0].FindControl("RadTreeView1") as RadTreeView;
        }
    }
 
    public class SelectorTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
        }
    }
 
    private ITemplate template;
    private CmsManager manager;
 
}

My Selector template is shown below. I declared there a RadTreeView instance:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="PageIDWebEditor.ascx.cs" Inherits="CustomControls_PageIDWebEditor" %>
 
 
<asp:Label runat="server" ID="Label1" Text="Select a page from the tree" />
<telerik:RadTreeView ID="RadTreeView1" runat="server" Skin="Black"/>

In the code behind I have to disable the NavigateUrl as otherwise when a node is selected a postback will appear, which will clear the selected nodes collection.

public partial class CustomControls_PageIDWebEditor : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RadTreeView1.NodeDataBound += new Telerik.Web.UI.RadTreeViewEventHandler(RadTeerView1_NodeDataBound);
    }
 
    void RadTeerView1_NodeDataBound(object sender, Telerik.Web.UI.RadTreeNodeEventArgs e)
    {
        foreach (RadTreeNode node in RadTreeView1.GetAllNodes())
        {
            node.NavigateUrl = "";
        }
    }
}

My selector is ready and now I need a custom control. The custom control will inherit from the well know CompositeControl class. This custom control does not need a template, because the SiteMenu will be dynamically generated.

I need a ViewState property from where I will call the WebUITypeEditor. I will use the built-in GuidArrayConverter, so that I will not have to create a custom DictionaryConverter for my property that is not just a simple string

[WebEditor("DynamicMenuBuilderWebUITypeEditor, App_Code")]
    [TypeConverter("Telerik.Framework.Utilities.GuidArrayConverter, Telerik.Framework")]
    public Guid[] PageIDs
    {
        get
        {
            object obj = this.ViewState["PageIDs"];
            if (obj != null)
                return (Guid[])obj;
            return new Guid[0];
             
        }
        set
        {
            this.ViewState["PageIDs"] = value;
        }
    }

I will override CreateChildControls() where I will dynamically create a RadMenu control and List of all selected pages. Then, I will bind the menu to this list similar to the way that is used in the custom selector to populate the RadTreeView control.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using Telerik.Cms.Web.UI;
using System.ComponentModel;
using Telerik.Web.UI;
using Telerik.Cms;
 
/// <summary>
/// Summary description for DynamicMenuBuilder
/// </summary>
public class DynamicMenuBuilder : CompositeControl
{
    public DynamicMenuBuilder()
    {
 
    }
 
    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        RadMenu menu = new RadMenu();
        var Manager = new CmsManager();
        List<ICmsPage> selectedPages = new List<ICmsPage>();
 
        foreach (Guid g in PageIDs)
        {
            ICmsPage p = Manager.GetPage(g, false) as ICmsPage;
            if (p != null)
            {
                selectedPages.Add(p);
            }
        }
        menu.DataSource = selectedPages;
        menu.DataTextField = "MenuName";
        menu.DataFieldParentID = "ParentID";
        menu.DataFieldID = "ID";
        menu.DataNavigateUrlField = "StaticUrl";
        menu.DataBind();
        Controls.Add(menu);
    }
 
 
    [WebEditor("DynamicMenuBuilderWebUITypeEditor, App_Code")]
    [TypeConverter("Telerik.Framework.Utilities.GuidArrayConverter, Telerik.Framework")]
    public Guid[] PageIDs
    {
        get
        {
            object obj = this.ViewState["PageIDs"];
            if (obj != null)
                return (Guid[])obj;
            return new Guid[0];
             
        }
        set
        {
            this.ViewState["PageIDs"] = value;
        }
    }
}

57 Comments

  1. 1 KingKong 15 Feb
    Ivan, thank you for wonderful blog post. Using this selector I can now create custom navigation and easily choose which pages to show. It same me a lot of time trying to figure it out by myself.
  2. 2 Romi 19 Feb
    Kudos Ivan,
    Always more than expected ! It's possible to use as source for RadSitemap?. I have some items in my wish list if you like, one module using OpenAccess, and docks using v3+. Thanks, Romi.
  3. 3 dissertation writing ning blog 02 Mar
    Great blog! The information you provide is quiet helpful, why I was not able to find it earlier. Anyways I’ve subscribed to your feeds, keep the good work up.





    http://dissertationwriting.ning.com/
  4. 4 dissertation blogstream blog 29 Mar
    Great Blog I am Certified to your blog Because I have Searched and visited blog contents are Unique. if your blog were not post unique so I would not post my comment.

    http://dissertations.blogstream.com/
  5. 5 Halloween costumes 31 Jul
    There are many cases where you want to have two level navigation or several navigational  menus. Each time you will have to do some coding over the menu so that it will start showing the appropriate items/nodes.
    http://www.hullabaloocostumes.com/
  6. 6 Halloween costumes 31 Jul
    thank you for wonderful blog post. Using this selector I can now create custom navigation and easily choose which pages to show. It same me a lot of time trying to figure it out by myself.
    http://www.hullabaloocostumes.com/

  7. 7 Piano Lessons 02 Aug
    Great Blog I am Certified to your blog Because I have Searched and visited blog contents are Unique. if your blog were not post unique so I would not post my comment.
    Piano Lessons
  8. 8 Mealworms 08 Aug
    There are many cases where you want to have two level navigation or several navigational  menus. Each time you will have to do some coding over the menu so that it will start showing the appropriate items/nodes.
  9. 9 sathi 16 Dec
    I will bind the control to a list containing all pages by using the CmsManager.GetPages() method. Then I will return the ID
    Dog Boots
  10. 10 Christy Turlington 30 Dec
    Very nice Site number one topic Thanks you..
    online diploma
    associate degree programs
    bachelor degree programs
  11. 11 AAAAAA 14 May
    Hi
  12. 12 henry 19 Sep
    <a href="http://www.online-essays-help.co.uk/essay-writing/buy-custom-essays.htm">where to buy good quality essays</a>/<a href="http://www.online-essays-help.co.uk/essay-writing/buy-custom-essays.htm">where to buy essay online</a>Top notch Essay Writing Services for students who value their time and money.
  13. 13 henry 15 Dec
      I have some items in my wish list if you like, It same me a lot of time trying to figure it out by myself.dissertation writing advice
  14. 14 kash 14 Jan

    Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.


    professional research paper writers

  15. 15 tn pas cher 16 May
    yed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.
  16. 16 readf 19 Jun
    Each time you will have to do some coding over the menu so that it will start showing the appropriate items/nodes.<a href="http://www.bestbusinesslogodesignn.com/">business logo design</a>
  17. 17 James 19 Oct
    I have visited to this site many times and everytime I find valuable jobs for me so I would suggest please come to this site and take the chance from here.
    <a href="http://www.bestbingopages.com/">bingo bingo</a> || <a href="http://www.bestcasinocontent.com/">casino spiele kostenlos spielen</a>
  18. 18 Juliet 23 Oct
    site menu builder? What is that then? What is the purpose of having something like that. Everything will then be alright. Sell Electrical Surplus
  19. 19 Richard 23 Oct
    For me,, I think we have nothing to do about it but we can always edit it if we are doing the right thing. Exam Central
  20. 20 Angeline 26 Oct
    Glad I made it at last. Thanks for reminding me with these codes here. i have only one lack of special character that is why I got to finish my project the sooner. it's really the best one.
    Thanks,
    <a href="http://www.wordpress-montreal.net/services">wordpress web development</a>
  21. 21 Angeline 26 Oct
    Glad I made it at last. Thanks for reminding me with these codes here. i have only one lack of special character that is why I got to finish my project the sooner. it's really the best one.
    Thanks, wordpress web development
  22. 22 Miami Printing 26 Oct
    These codes will help me improve my site. I only hope it will work on my end. :)
  23. 23 Today only 28 Oct
    I really enjoyed this site.
    Your newer posts are simply wonderful compared to your posts in the past. Keep
    up the good work!
    <a
    href="http://www.tantricandthecity.com">Today only</a>
  24. 24 East London 28 Oct
    Every time I see a really
    good article I do a few things:1.Share it with all the relevant friends.2.save
    it in all of the favorite social sharing sites.3.Make sure to return to the
    blog where I read the post.After reading this post I am seriously concidering
    doing all 3!
    <a href="http://www.thetantricway.com">East London</a>
  25. 25 Russian Shawls 31 Oct
    I had so many questions on this topic, but your generous helped me to find answers for almost all of them. Thanks for sharing a nice information
  26. 26 mens satin shirts 31 Oct
    Hi there! I simply wish to give an enormous thumbs up for the nice info you’ve here on this post. I will be coming again to your weblog for extra soon
  27. 27 Lipozene Reviews 01 Nov
    I like the cut
    of your job :) or at least your thought process but sorry to say, I honestly
    think you would have fully sold me on the idea had you been able to back up
    your premis with a substantial bit more solid facts.

  28. 28 DorisGreys 05 Nov
    This is a very informative article, and it helped me a lot when working on my project for a <a href="https://www.whiteknightcasino.com/">No Download Casino</a>
  29. 29 communication expert 09 Nov
    Sitemenu Builder? I guess I can also make something like that for my own websites. I have to also make deal with it and thanks a lot for all the help. This is all what i really wanted.
  30. 30 beef jerky 15 Nov
    Having a website that looks nice is only part of the internet marketing package.
    Indeed it was an awesome post. It endows me with worthwhile information that
    definitely increases my awareness on this particular subject. For the person
    posted this editorial for the benefits of the general public thank you for
    sharing.
  31. 31 How to Fillet a Salmon 17 Nov
    Well, of course then and  guess I got to learn from it and everything will then be alright. Thanks a lot for all the help and it will be just so awesome. 
  32. 32 informationonline.com 20 Nov
    I am not really a web developer that is why I am not really an expert in creating websites, and etc... so right now I am looking for more information about it, search some resources to be used because I want to learn and I want to change my career.
  33. 33 multiple choice question answers 27 Nov
    Nothing is more absolutely frustrating to
    any person than needing to write one's thoughts clearly and concisely on paper,
    in the form of a personal letter to a friend or loved one, a statement of
    purpose required in a university application, a professional summation of job
    performance required by a manager, or an essay, research paper, or thesis
    required by a high school instructor or college professor for a course grade,
    and not having the fundamental skills to do so.
  34. 34 English speaking 01 Dec
    This blog contains a lot of informative data and news which i first
    time read from here. Thanks for writing about this important topic.

     
  35. 35 american poker online 09 Dec
    Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work! <a href="http://www.onlinecasinobonusmeister.com/">american poker online</a> | <a href="http://www.onlinecasinobonussichern.com/">novoline online spielen</a>
  36. 36 paracord bracelet 13 Dec
    This post is truly inspiring. I like your post and everything you share with us is current and very informative,
  37. 37 doris 20 Dec
    Informative blog...thanks for share !!!
    http://www.rittersprinting.com/
  38. 38 praveen 21 Dec
    Despite the earlier plan for days, had to spend a day in a casino, today it is possible at any time to log into an online casino jokerscap | Dragons Treasure online and there to play their favorite games. While this is a great advantage for the one, but there are also others which can quickly do a gambling wide For this reason, you should always perform a spielsucht test, to be sure that the games runs in the normal course spielsucht test: gambling is nowadays a major problem. Perhaps the greatest problem is that the act itself is not affected, that they are addicted to the games. One makes really no big thoughts about their own gambling behavior, but always thinks that this is just pure fun and pleasure. But how can you find out for yourself whether you are affected by compulsive gambling or not? The best way to do this, even if you perform a gambling addiction test. Meanwhile, there are even a few websites where you can free to carry out such tests. These are not just for new players, but also players out there, for months or even years to an account with large and well-known online casino should have to regularly perform such a gambling addiction test.
  39. 39 pmi-acp certification 02 Jan

    I've been looking for this for a couple of an hour and I am so glad that you have shared this, thanks it really helps me.

  40. 40 pmp flash cards 02 Jan

    I've copied all the programming text here but when I am trying to save it, I just found some errors, I don't know where is it, I cannot trace it back.

  41. 41 tefl certification online 03 Jan
    I also encountered error when I paste this code, lacking comma, I cannot trace it back where part of the code that the comma is missing.
  42. 42 online teaching jobs 03 Jan
    Please consider in copying again the code and paste it again, let see if you encounter the same problem.
  43. 43 paralegal certification online 04 Jan
    The paralegal profession is growing massively as lawyers look to trim costs, but deliver great service. Paralegals provide much important 'back office' work, but increasingly they are handling everything from jury selection issues to divorce, bankruptcy and litigation documentation. And paralegals are well paid and can qualify for their paralegal certification online. Here's what you need to know.
  44. 44 best diet to be healthy 22 Jan
    What kind of menu builder would that be? Is it something that we should be count on? or is it a user friendly or something? can you tell us more?
  45. 45 test fixtures 01 Feb

    What does that site menu builder mean? Does it really mean a lot to you? i know for sure things will be so alright and awesome if I am going to learn from it.

  46. 46 Business Attorney Minneapolis, MN 07 Feb
    If you are living in Minnesota or the twin cities you have to be a 
    little more careful with your car and your driving. Drunk driving should
    be a complete no for you. If you're ever charged for DWI or DUI, here 
    is all that could happen to you under the Minnesota laws.
  47. 47 Dentist South Jordan 13 Feb

    "This post will show you how to create custom navigation menu for your website, where you can specify which pages you want to see and which not. "

    - This is how I would just always wanted to do and I am sure things would really go on that way.

  48. 48 online pdu 19 Feb
    It is fantastic blog providing you with us something to laugh. I find myself really fresh after every post for you personally. Keep providing us such posts. Thanks for this.
  49. 49 free blogs 28 Feb
    This article has some great and useful information about this subject.
    Thank you for sharing it in an easy to read and understandable format.
    Thanks for sharing this great information.
  50. 50 Expart Linker 02 Mar
    I am so glad that I found an interesting and informative post like yours.
    Several of the blogs that I've seen in these previous days did not please me
    due to insufficient substance.

     <a href="http://www.43things.com/entries/view/5039348">bestessays scam</a>

  51. 51 Expart Linker 02 Mar
  52. 52 rikki 05 Mar

    read more here whenever I am searching for awesome content and ideas around the net, I always this is an awesome place for that.

     

  53. 53 JasonTheMan 06 Mar

    Your style is very unique compared to other folks I've read stuff from. Thank you for posting when you've got the opportunity, Guess I'll just bookmark this web page

  54. 54 NIKE 18 Mar
    I just want to say thanks for your wonderful post, it is contain a lot
    of knowledge and information that i needed right now. You really help me
    out my friend, thanks !
  55. 55 besuchen Sie 09 Apr
    Theres for sure a bundle to find out about this detox thing. I assume you have some kind of experience in this field. 
  56. 56 Alburt Green 19 Apr
    It is not right that every new thing attract a person. but
    it is quite set for the situation when I first noted this site it is nice for
    those want to get interesting information for there spare time.<a href="http://www.crunchbase.com/company/rush-essay">rushessay reviews</a>

  57. 57 rushessay reviews 19 Apr
    This post has shared the information about the unique
    creature. I really like this post and I used to see these types of creatures on
    TV. Thanks for updating the information like this.

Comment

  1.    
     
     
      
       

Categories

+ View all

Blogs

+ View all