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

Forums / Developing with Sitefinity / Accessing Page Description from ascx

Accessing Page Description from ascx

5 posts, 1 answered
  1. Alex
    Alex avatar
    1 posts
    Registered:
    27 Oct 2011
    23 Jun 2012
    Link to this post
    Hi,

    I'm trying to migrate the Simplicity template to 5.x, and I have a problem with their custom navigation control. See the code below:

    <%@ Control Language="C#" AutoEventWireup="true" %>
    <%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI.NavigationControls" TagPrefix="sfMap" %>
    <telerik:RadTabStrip ID="RadPanelbarNav" runat="server" Visible="false" />
      
    <asp:Repeater  ID="Repeater1" runat="server" DataSourceID="SiteMapDataSource" >
        <HeaderTemplate>
            <ul class="nav-holder">
        </HeaderTemplate>
        <ItemTemplate>
                <li>
                    <a href='<%# Eval("Url") %>' runat='server'>
                        <h3><%#Eval ("Title") %></h3>
                        <p><%#Eval("Description")%></p>
                    </a>
                </li>
        </ItemTemplate>
        <FooterTemplate>
            </ul>
        </FooterTemplate>
    </asp:Repeater>
      
    <sfMap:SitefinitySiteMapDataSource runat="server" ID="SiteMapDataSource" StartingNodeOffset="0" ShowStartingNode="false" />

    I already replaced the SiteMapDataSource by the new SitefinitySiteMapDataSource, but now the expression <%#Eval("Description")%>  fails because there isn't such a property in the items provided by the datasource.

    Is there a way to access the page description from the ascx file without code-behind?

    Thank you,

    Alexis
  2. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    24 Mar 2017
    28 Jun 2012
    Link to this post
    Hello,

    That's a tough one, unfortunately there is no way to access the Description field on a PageSiteNode if you don't get the PageNode itself - SitefinitySiteMapDataSource is a special control we've developed to provide the best performance when working with custom navigation controls, as it gives you the cached output from the pages provider, and also takes care of  things like ShowInNavigation, group pages behavior etc.

    However you can easily subscribe to the repeater ItemDataBound event and do something like:
    protected void Page_Load(object sender, EventArgs e)
            {
                Repeater1.ItemDataBound += new RepeaterItemEventHandler(Repeater1_ItemDataBound);
            }
        
            void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
                {
                    var item = e.Item.DataItem as PageSiteNode;
                    var pageManager = PageManager.GetManager();
                    PageNode pageNode;
                    using (new ElevatedModeRegion(pageManager))
                    {
                        pageNode = pageManager.GetPageNode(item.Id);
                    }
                    Label descrLabel = e.Item.FindControl("descrLbl") as Label;
                    if (descrLabel != null && pageNode.Page != null)
                    {
                        descrLabel.Text = pageNode.Page.Description.ToString();
                    }
                }
            }



    Regards,
    Boyan Barnev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
  3. Richard
    Richard avatar
    64 posts
    Registered:
    09 Oct 2012
    20 Sep 2012
    Link to this post
    Basically what I need is the description (or something else) in addition to just the page title. I found this thread and it seemed like it would work, unfortunately I'm brand new to Sitefinity, C#sharp, and I'm pretty much a front front end guy to boot. So how would I use the code you've suggested above (meaning where would I put it). Do I need a codefile in addition to the MainNavigation.ascx that Simplicity uses? or where would I put that code? Do you have another suggestion?

    Thanks for your help!
  4. Boyan Barnev
    Boyan Barnev avatar
    1429 posts
    Registered:
    24 Mar 2017
    21 Sep 2012
    Link to this post
    Hello Richard,

    Yes, the sample is actually the codefile added to the ascx template for MainNavigation.ascx.
    Please note you will need to add a label with ID descrLbl on the frontend, which will be the container for the Description text.

    Greetings,
    Boyan Barnev
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Richard
    Richard avatar
    64 posts
    Registered:
    09 Oct 2012
    21 Sep 2012
    Link to this post
    Hey Boyan, Thanks for the input. I put the sample code in a codefile and I'm getting an error 
    Compiler Error Message: CS1518: Expected class, delegate, enum, interface, or struct 

    Clearly I'm clueless :) I must be missing something in my codefile...

    Thanks for you help!
5 posts, 1 answered