More in this section

Forums / Developing with Sitefinity / Best Page Selector for Custom Control

Best Page Selector for Custom Control

4 posts, 0 answered
  1. Gavin
    Gavin avatar
    20 posts
    Registered:
    06 Jul 2009
    12 Oct 2009
    Link to this post
    Hi,

    I'm creating a custom control which can link to another page within Sitefinity. What is the best built-in selector to use for this?

    I understand that it's probably best to use one that returns the pageId, as if the page changes location later I should be able to use the pageId to retrieve it's current url.

    I've tried CmsUrlWebEditor and UrlEditorWrapper but both return a url instead of the pageID.

    Is there a list anywhere describing the built-in selectors and when they are best used, advice on using them etc.?

    Cheers,
    Gavin
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    12 Oct 2009
    Link to this post
    Hi Gavin Harriss,

    There is not built in selector that can be used to return a pageID instead of its relative path. There are two options

    1. Use the following WebEditor declaration.

    [WebEditor("Telerik.Cms.Web.UI.DhlUrlEditor, Telerik.Cms")]
       [Browsable(true)]
       [Category("Navigation")]
     
       public string NavigateUrl
       {
           get
           {
               return this.navigateUrl;
           }
           set
           {
               this.navigateUrl = value;
           }
       }
       private string navigateUrl;
     
       [TypeConverter("Telerik.Cms.Web.UI.GuidTypeConverter, Telerik.Cms"),
       WebEditor("Telerik.Cms.Web.UI.DhlIdEditor, Telerik.Cms"),
       DefaultValue(typeof(Guid), "00000000-0000-0000-0000-000000000000"),
       Category("Custom")]
       public Guid PageId
       {
           get
           {
               return this.pageId;
           }
           set
           {
               this.pageId = value;
           }
       }
       private Guid pageId;


    2. Create a custom page ID selector as this one shown below:

    2.1.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Cms.Web.UI;
    using System.Web.UI;
    using Telerik.Framework.Web;
    using Telerik.Web.UI;
    using Telerik.Cms.Web;
    using Telerik.Cms;
    using System.Collections;
     
    /// <summary>
    /// Summary description for PageIDWebEditor
    /// </summary>
    public class PageIDWebEditor : WebUITypeEditor<Guid>
    {
        public PageIDWebEditor()
        {
        }
     
        public override Guid Value
        {
            get
            {
                Guid pageId = Guid.Empty;
     
                if (TreeView.SelectedNode != null)
                    pageId = new Guid(TreeView.SelectedNode.Value);
     
                if (pageId != Guid.Empty)
                    return ((ICmsPage)manager.GetPage(pageId)).ID;
     
                return Guid.Empty;
                 
            }
            set
            {
                this.ViewState["selectedPage"] = value;
                 
            }
        }
     
        // TEMPLATE PATH
        public string Templte
        {
            get
            {
                object o = this.ViewState["Template"];
                if (o == null)
                    return ("~/CustomControls/PageIDWebEditor.ascx");
                return (string)o;
            }
            set
            {
                this.ViewState["Template"] = value;
            }
        }
     
        // CONTROL REFERENCE
        protected virtual RadTreeView TreeView
        {
            get
            {
                return this.Controls[0].FindControl("RadTreeView1") as RadTreeView;
            }
        }
     
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
     
            this.manager = new CmsManager();
            this.template = ControlUtils.GetTemplate<SelectorTemplate>(this.Templte);
            this.template.InstantiateIn(this);
            RadTreeNode root = new RadTreeNode();
            root.Value = Guid.Empty.ToString();
            root.Text = "AllPages";
            root.Expanded = true;
            TreeView.Nodes.Add(root);
            this.LoadNodes(root);
        }
     
        private void LoadNodes(RadTreeNode root)
        {
                ICmsPage sPage = null;
                CmsManager manager = new CmsManager();
                 
                foreach (ICmsPage page in manager.GetPages(new Guid(root.Value)))
                {
                    RadTreeNode node = new RadTreeNode();         
                    node.Value = page.ID.ToString();
                    node.Expanded = true;
                    node.Text = page.MenuName;
                    node.ToolTip = page.MenuName;
                    root.Nodes.Add(node);        
                }
                 
        }
     
        
        public class SelectorTemplate : ITemplate
        {
            public void InstantiateIn(Control container)
            {
     
            }
        }
        private ITemplate template;
        private CmsManager manager;
    }


    2.2. Template


    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="PageIDWebEditor.ascx.cs" Inherits="CustomControls_PageIDWebEditor" %>
     
    Page ID selector
     
    <telerik:RadTreeView ID="RadTreeView1" runat="server"/>


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Telerik.Cms.Web;
    using Telerik.Web.UI;
     
    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 = "";
            }
        }
    }

    We should have the following declaration of our custom property

    [TypeConverter("Telerik.Cms.Web.UI.GuidTypeConverter, Telerik.Cms"),WebEditor("PageIDWebEditor, App_Code")]
    public Guid CustomPageIDSelector
    {
        get
        {
            return this.CustomPageID;
        }
        set
        {
            this.CustomPageID = value;
        }
    }
     
    private Guid CustomPageID;


    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Gavin
    Gavin avatar
    20 posts
    Registered:
    06 Jul 2009
    13 Oct 2009
    Link to this post
    Hi Ivan, thanks for that - both solutions work very well :)
  4. Gavin
    Gavin avatar
    20 posts
    Registered:
    06 Jul 2009
    13 Oct 2009
    Link to this post
    Actually, while I was using Reflector looking for a link resolver I actually came across a built in web editor that does exactly what I want...

    [Category("Navigation"), WebEditor("Telerik.Cms.Web.UI.CmsHyperLinkUrlWebEditor, Telerik.Cms")]
    public string TargetCmsPageID { get; set; }
Register for webinar
4 posts, 0 answered