+1-888-365-2779
Try Now
More in this section

Forums / Bugs & Issues / Setting ParentLevelsDisplayed on Breadcrumb

Setting ParentLevelsDisplayed on Breadcrumb

8 posts, 0 answered
  1. Liam
    Liam avatar
    18 posts
    Registered:
    30 Jun 2010
    22 Jul 2010
    Link to this post
    Hello,

    I experienced a problem where I wanted to programmatically set the ParentLevelsDisplayed property on a telerik Breadcrumb that existed on a master page. However, if I hard code a value in the mark-up I found that it worked.

    So just to clarify, I have a telerik Breadcrumb in the mark up of my master page. In some code, I find find that Breadcrumb control usinf the FindControl method. I wanted to set its ParentLevelsDisplayed property to some value. In the debugger, I could see that it was being set but when viewing pages it had no effect and I simply got the default behaviour. As a test I changed the background colour and I could see that working, so I was accessing the Breadcrumb. And if I set ParentLevelsDisplayed in the mark up then it did work.

    My solution was to remove the Breadcrumb and instantiate a new one, set ParentLevelsDisplayed and add it at the same location as the old one. In that case all worked fine.

    Cheers
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    22 Jul 2010
    Link to this post
    Hi Liam,

    Thank you for using our services.

    I have added the control like this on my master page:
    <div id="BreadCrumbDiv">
        <telerik:Breadcrumb ID="Breadcrumb1" runat="server" />
    </div>

    and then on the page load event I set the property and it works properly:
    <script runat="server">
        protected void Page_Load()
        {
            Breadcrumb1.ParentLevelsDisplayed = 2;
        }
    </script>

    Can you please show us the piece of code which you are using. Also please let us know in which event of the page/control life cycle you are setting the property. It might be the case that you are setting it too late and the control has been already data bound.

    All the best,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Liam
    Liam avatar
    18 posts
    Registered:
    30 Jun 2010
    23 Jul 2010
    Link to this post
    Hi Radoslav,

    Thanks for your prompt reply.

    Originally I made the breadcrumb as a user control and did what I needed to, including setting the ParentLevelsDisplayed property, in the code behind for that user control and it worked fine.

    However, I had several controls that I had created like this and I decided that instead of having my users have to place several controls onto a template I would hard code the controls in the master page and create a single control that would manage aspects of all those controls.

    So my setup is as follows:

    On the master page:
          <div id="breadcrumb">
             <telerik:Breadcrumb ID="siteBreadcrumb" EnableViewState="false" runat="server"></telerik:Breadcrumb>
          </div>

    And I have a user control (whose purpose is act as a single place to manage the controls on the master page) that
    inherits from a class TemplateProperties in which I want to set properties of the breadcrumb:
    public class TemplateProperties : System.Web.UI.UserControl
    {
       private string breadCrumbId = "siteBreadcrumb";
       ...
      
       protected override void OnInit( EventArgs e )
       {
          base.OnInit(e);
      
          if( ((CmsPageBase)Page).PageMode==CmsPageMode.Live )
          {
             /// Find the home node
             homeNodeFound = CmsAdmin.FindHomeNode(SiteMap.CurrentNode, out home);
      
             // Initialise the breadcrumb
             InitBreadcrumb(breadCrumbId);
      
             // Initialise other controls ...
          }
       }
      
       private void InitBreadcrumb(string controlId_)
       {
          Breadcrumb _breadcrumb = (Breadcrumb)this.Page.Master.FindControl(controlId_);
          _breadcrumb.ParentLevelsDisplayed = 1;
          _breadcrumb.BackColor = System.Drawing.Color.Lime;
       }
    }

    I have simplified the InitBreadcrumb method above for clarity, but my experience was that the back colour did get set but setting PerentLevelsDisplayed had no effect.

    So I'm trying to set the breadcrumb properties OnInit, but I did also experiment and tried setting ParentLevelsDisplay both OnLoad and OnPreRender and still neither worked.

    My current solution is:
    private void InitBreadcrumb(string controlId_)
    {
       Breadcrumb _breadcrumbPlaceholder = (Breadcrumb)this.Page.Master.FindControl(controlId_);
       Control _breadcrumbContainer = _breadcrumbPlaceholder.Parent;
       int _breadcrumbIndex = _breadcrumbContainer.Controls.IndexOf(_breadcrumbPlaceholder);
       _breadcrumbContainer.Controls.Remove(_breadcrumbPlaceholder);
        
       // The telerik Breadcrumb control that will be inserted into the container
       Breadcrumb _breadcrumb = new Breadcrumb();
       _breadcrumb.ID = controlId_;
       _breadcrumb.ParentLevelsDisplayed = _levelsUp;
       _breadcrumbContainer.Controls.AddAt(_breadcrumbIndex, _breadcrumb);
    }


    Thanks for your help.
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    23 Jul 2010
    Link to this post
    Hi Liam,

    I am unable to reproduce the issue. Make sure that you do not have any code leftover that might be changing the breadcrumb after your initialize breadcrumb method is executed. I have attached the master page and the user control which I have used to test the behavior.

    Greetings,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Liam
    Liam avatar
    18 posts
    Registered:
    30 Jun 2010
    23 Jul 2010
    Link to this post
    Hi Radoslav,

    Thanks again for looking at this. I realise it's a tricky one.

    Now with the example you have there you have hard-coded the WebUserControl into the master page, so I'm not surprised that that approach works. Using you code, I have modified it slightly and reproduced the behaviour.

    So please try this:
    * don't hard-code the WebUserControl onto the master page but rather use the 'Upload a control' interface in edit mode to upload it as a user control and drop it onto the template in edit template mode.
    * create a hierarchy of pages several levels deep that use the template.

    So the master page should be:
    <%@ Master Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     
    <script runat="server">
    </script>
     
    <head runat="server">
        <title></title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />    
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <div id="wrapper">
            <div id="Header">
                <h1>My Organization</h1>
                <div id="TopNavigation">
                    <asp:contentplaceholder id="TopMenu" runat="server"></asp:contentplaceholder>           
                </div>
                <div id="breadcrumb">
                    <telerik:Breadcrumb ID="siteBreadcrumb" EnableViewState="false" runat="server"></telerik:Breadcrumb>
                </div>
            </div>
            <div id="Main">
            <asp:contentplaceholder id="Content" runat="server"></asp:contentplaceholder></div>
            <div id="SideBar"><asp:contentplaceholder id="SideBarContent" runat="server"></asp:contentplaceholder></div>   
            <div id="Footer">2006-2007 © MyOrganization. All rights reserved </div>
           </div>
       </form>
    </body>
    </html>
    The WebUserControl can remain unchanged but upload it as a user control and drop it onto the template.

    Thanks again.
  6. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    23 Jul 2010
    Link to this post
    Hi Liam,

    The reason for what is happening is that the breadcrumb control is already initialized and data bound when the piece of code from the user control executes. In order to make this work in this scenario you have to rebind the breadcrumb. Something like the code bellow will do the trick:
    private void InitBreadcrumb(string controlId_)
    {
        Breadcrumb _breadcrumb = (Breadcrumb)this.Page.Master.FindControl(controlId_);
        _breadcrumb.Controls.Clear();
        _breadcrumb.ParentLevelsDisplayed = 0;
        _breadcrumb.DataBind();
        _breadcrumb.BackColor = System.Drawing.Color.Violet;
    }


    Kind regards,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Liam
    Liam avatar
    18 posts
    Registered:
    30 Jun 2010
    28 Jul 2010
    Link to this post
    Hi Radoslav,

    Thanks, that seems to do the trick.

    Just out of curiosity, I wonder why the difference in behaviour for the two approaches?

    Thanks for your help.

    Regards,
    Liam
  8. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    28 Jul 2010
    Link to this post
    Hello Liam,

    The difference is in the page and control life cycle. When you have the control as in the first sample I have sent the code executes before the bread crumb control has been bound (filled up with nodes). In the case where you are trying to configure it from a control which is added through the Sitefinity page editor, the code in the control executes in a stage where the bread crumb has been bound and changing the property will not change its output unless you rebind it.

    Kind regards,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
8 posts, 0 answered