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

Forums / Developing with Sitefinity / Page header: Display a Root Menu item Page Name in a Label on the page

Page header: Display a Root Menu item Page Name in a Label on the page

8 posts, 0 answered
  1. Matthew
    Matthew avatar
    38 posts
    Registered:
    24 Jun 2012
    28 Oct 2008
    Link to this post
    Hi there,

    Looking for some examples/help on how to create a label on my master page, which is populated by the root menu item name for that particular section of the site. e.g. if I browse to any of the following pages in my example below, the label on my master page would be populated with "Our Products"

    example:

    Our Products
    - Product 1
    - - Sub Product 1
    - - Sub Product 2
    - Product 2
    - Product 3
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    28 Oct 2008
    Link to this post
    Hello Matthew,

    Here is an example that demonstrates the desired functionality. The Label on the master page will be populated with the root page title.

    In this example we have the code below in a master page.

    <script runat="server"
    void Page_Load(object sender, EventArgs e) 
        SiteMapNode node = SiteMap.CurrentNode; 
        if(node != null) 
            pageName.Text = this.GetRoot(node).Title; 
             
             
        if (this.Page != null) 
            pageTitle.Text = this.Page.Title; //show <head> name title 
     
    private SiteMapNode GetRoot(SiteMapNode node) 
        if (node.ParentNode == SiteMap.RootNode) 
            return node; 
        else 
            if (node.ParentNode.ParentNode != null) 
                return GetRoot(node.ParentNode); 
            else 
                return node.ParentNode; 
    </script> 
     
    .... 
     
    <asp:Literal runat="server" ID="pageName"></asp:Literal><br /> 
    <asp:Literal runat="server" ID="pageTitle"></asp:Literal> 

    I hope this helps.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Matthew
    Matthew avatar
    38 posts
    Registered:
    24 Jun 2012
    29 Oct 2008
    Link to this post
    thank you!
  4. Marko
    Marko avatar
    148 posts
    Registered:
    30 Jul 2008
    05 Nov 2008
    Link to this post
    This seems to work while browsing the website, but in the CMS admin area, I get the following error:

    System.NullReferenceException: Object reference not set to an instance of an object.

    This is the line taht causes it:
    if (node.ParentNode.ParentNode != null)
    But the circumstances under which the error occurs are weird:
    1. In Admin, under Pages, select a page that's running this code, click Edit.
    2. In Page Editor mode, just click Cancel
    3. Click Edit button again, and then you are presented with the error.

    Any ideas?
  5. Matthew
    Matthew avatar
    38 posts
    Registered:
    24 Jun 2012
    05 Nov 2008
    Link to this post
    Hi Marko,

    I got the same error in admin and just added a try catch block as follows - however telerik will most likely offer a more elegant solution :)

    <script runat="server">  
    void Page_Load(object sender, EventArgs e)  
    {  
        SiteMapNode node = SiteMap.CurrentNode;  
        if(node != null)  
            pageName.Text = this.GetRoot(node).Title;  
              
              
       // if (this.Page != null)  
       //     pageTitle.Text = this.Page.Title; //show <head> name title  
    }  
      
    private SiteMapNode GetRoot(SiteMapNode node)  
    {  
        if (node.ParentNode == SiteMap.RootNode)  
            return node;  
        else 
     
            try 
            { 
                if (node.ParentNode.ParentNode != null) 
                    return GetRoot(node.ParentNode); 
                else 
                    return node.ParentNode; 
            } 
            catch (Exception e) 
            { 
                //todo - object not set to an instance of an object in admin. 
                return node; 
            } 
    }  
         
    </script>  
     

  6. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    07 Nov 2008
    Link to this post
    Hi,

    I would recommend you modify the code as it is shown below instead of using try-catch block:

    <script runat="server">  
    void Page_Load(object sender, EventArgs e)  
    {  
        SiteMapNode node = SiteMap.CurrentNode; 
        if (node != null && node.ParentNode != null)  
            pageName.Text = this.GetRoot(node).Title;  
              
              
        if (this.Page != null)  
            pageTitle.Text = this.Page.Title; //show <head> name title  
    }  
      
    private SiteMapNode GetRoot(SiteMapNode node)  
    {  
        if (node.ParentNode == SiteMap.RootNode)  
            return node;  
        else 
            if (node.ParentNode.ParentNode != null)  
                return GetRoot(node.ParentNode);  
            else  
                return node.ParentNode;  
    }  
    </script> 
     
    ... 
     
    <asp:Literal runat="server" ID="pageName"></asp:Literal><br />  
    <asp:Literal runat="server" ID="pageTitle"></asp:Literal> 

    Hope this helps.

    Greetings,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  7. Marko
    Marko avatar
    148 posts
    Registered:
    30 Jul 2008
    07 Nov 2008
    Link to this post
    OK, thanks. That seems to work OK.  And thanks to Matthew, too. The try-catch was a good temporary solution :-)
  8. thomas3577
    thomas3577 avatar
    22 posts
    Registered:
    18 Feb 2007
    08 Sep 2009
    Link to this post
    Thanks! :-)
Register for webinar
8 posts, 0 answered