More in this section

Forums / Developing with Sitefinity / Getting saved control properties out of parent pages

Getting saved control properties out of parent pages

3 posts, 0 answered
  1. UI Crew
    UI Crew avatar
    151 posts
    Registered:
    27 Sep 2012
    15 Jan 2009
    Link to this post
    Hi,

    We are developing a site for a client at the moment where they need to be able to set a header image anywhere in the site and have that header image inherited by child pages until it is set again to something different.

    Setting the images in templates is not suitable because there are 5 possible page layouts and probably as many as 15 different header images which would result in too many templates. It is a large site too so we don't want to have them have to put a header image control in every page of the site.

    So to combat this I have created a custom control called HeaderImage which the content manager can put in a page and pick a header image from the Images and Docs module. The idea is that they will only put this in a page when they want to change the header image for that page and it's child pages. Of course all first level pages will have this control.

    Here is my HeaderImage custom control:

    using System;
    using System.ComponentModel;
    using System.Web.UI;
    using Telerik.Cms.Web.UI;

    namespace Bullseye.Sitefinity
    {
        [DefaultProperty("SelectImage")]
        public class HeaderImage : UserControl
        {
            [Category("Control Properties"), DisplayName("Header Image"),
             WebEditor("Telerik.Libraries.WebControls.ImageSelector, Telerik.Libraries")]
            public string SelectImage { get; set; }

            protected new void Page_Load(object sender, EventArgs e)
            {
                string mode = Request.QueryString.Get("cmspagemode");

                if (mode == "edit" || mode == "preview")
                {
                    if (string.IsNullOrEmpty(SelectImage))
                    {
                        Controls.Clear();
                        Controls.Add(
                            new LiteralControl(
                                "<p class=\"cmsDefaultEmptyControl\"><strong>Please note:</strong> A header image control has been placed but an image has not been selected. Click edit to select a header image to display.</p>"));
                        return;
                    }
                    else
                    {
                        Controls.Clear();
                        Controls.Add(
                            new LiteralControl(
                                "<p class=\"cmsDefaultEmptyControl\"<strong>Please note:</strong> A header image has been selected in this page and is being inherited by child pages.</p>"));
                        return;
                    }
                }
            }
        }
    }

    Pretty simple. Now what I want to do is in my masterpage have some logic that looks at the controls in the current page to see whether there is a HeaderImage control present and set the header image from the SelectImage property of the HeaderImage control. If there if the control is not present go back through each parent page until it finds a page with HeaderImage control and get the SelectImage property that has been saved for that page.

    Using ICmsWebControl I have been about to partially achieve the above and search through parent pages until I find a page with the HeaderImage control BUT once I have found the page I can't work out how I access the property and apply it as need to my current page.

    Here is the code in my masterpage that is doing the work:

       private CmsSiteMapNode CurrentCmsNode = (CmsSiteMapNode) SiteMap.CurrentNode;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (CurrentCmsNode == null) return;

            while (CurrentCmsNode != SiteMap.RootNode && CurrentCmsNode.ParentNode != null)
            {
                if (HasControl(CurrentCmsNode.GetCmsPage().Controls))
                {
                    foundControl(CurrentCmsNode);
                    break;
                }
                if (CurrentCmsNode.ParentNode == SiteMap.RootNode)
                {
                    break;
                }
                CurrentCmsNode = CurrentCmsNode.ParentNode as CmsSiteMapNode;
            }
        }

        private void foundControl(CmsSiteMapNode CurrentCmsNode)
        {
            //Just output the title of the page with the HeaderImage control so that I know this works in theory
            litTest.Text = CurrentCmsNode.Title;

            //Now that I have the CmsSiteMapNode with the Control in it how do I access the SelectImage property
            //so I can use it in my current page and set the requird image
            imgHeaderImage.Imageurl =
        }


        private static bool HasControl(IList<ICmsWebControl> Controls)
        {
            bool found = false;

            try
            {
                foreach (ICmsWebControl control in Controls)
                {
                    if (control.ControlType.ToString() == "Bullseye.Sitefinity.HeaderImage")
                    {
                        found = true;
                        break;
                    }
                }
            }
            catch
            {
            }

            return found;
        }

    How can I access the saved property? If this won't work is there another way?

    Thanks
  2. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    20 Jan 2009
    Link to this post
    Hi Seth Cleaver,

    Here is how you can access a control and a control property with our CmsManager:
    CmsManager manager = new CmsManager(); 
    IList allImageControls = manager.GetControls((typeof(Image)).ToString(), 
                                                             false); 
    foreach (ICmsWebControl cmsWebControl in allImageControls) 
      { 
            Image ImageCtrl = (Image)cmsWebControl.LoadControl(); 
            string imgSrc = ImageCtrl.ImageUrl; 
      } 

    In the particular example, I am using the Image control, but you can use any other type of control here.

    I hope this will point you in the right direction.

    All the best,
    Georgi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    04 Feb 2010
    Link to this post
    Hi Seth,

    This is something I also try to achieve.
    Do you already have a working solution?

    -Daniel-
Register for webinar
3 posts, 0 answered