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

Forums / Developing with Sitefinity / Category Name in ~/Sitefinity/ControlTemplates/News/Modes/ListPageMaster.ascx

Category Name in ~/Sitefinity/ControlTemplates/News/Modes/ListPageMaster.ascx

3 posts, 0 answered
  1. Bob Pinna
    Bob Pinna avatar
    13 posts
    Registered:
    10 Apr 2005
    18 Dec 2008
    Link to this post
    I have a NewsView control filtered on category name (I checked the category name in step 1 of the "Select which news to display" section of the basic property sheet).  Am I able to access the category name (or names if I had more than one category selected) from the control template?

    I'm using ~/Sitefinity/ControlTemplates/News/Modes/ListPageMaster.ascx and looking to add a header in the <HeaderTemplate> section that shows the category name.

    If this is possible, what is the ID I should use?  If not, then I'm assuming I would wrap the NewsView in a user control with a category name property exposed.  Is this the approach you would recommend?

  2. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    18 Dec 2008
    Link to this post
    Hi Bob,

    Please follow the instructions below to achieve the required functionality:

    1. Open ~/Sitefinity/ControlTemplates/News/Modes/ListPageMaster.ascx file.
    2. In the HeaderTemplate declare a repeater that will enlist all selected categories:

    <asp:Repeater ID="repeater" runat="server"
        <HeaderTemplate> 
            <ul class="sf_newsList"
                <asp:Repeater ID="Categories" runat="server"
                    <ItemTemplate> 
                        <strong><asp:Literal ID="categoryName" runat="server" /></strong
                    </ItemTemplate> 
                </asp:Repeater> 
        </HeaderTemplate> 
    ... 

    3. Create a new News View control extending from the base class - SetItemHeaderContent method should be overridden:

    using System; 
    using System.Data; 
    using System.Configuration; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.HtmlControls; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.WebControls.WebParts; 
    using Telerik.News.WebControls; 
    using Telerik.Cms.Engine; 
    using Telerik.Cms.Engine.ContentViewFiltering; 
    using System.Collections.Generic; 
     
    namespace Telerik.News.WebControls 
        public class NewsViewExtender : NewsView 
        { 
            protected override void SetItemHeaderContent(Control itemContainer, IContent contentItem) 
            { 
                base.SetItemHeaderContent(itemContainer, contentItem); 
     
                Repeater rep = itemContainer.FindControl("Categories"as Repeater; 
     
                if (rep != null
                { 
                    ContentFilterBuilder filterBuilder = new ContentFilterBuilder(this); 
                    List<string> categories = new List<string>(filterBuilder.ParseCategories()); 
     
                    rep.DataSource = categories; 
                    rep.ItemCreated += new RepeaterItemEventHandler(rep_ItemCreated); 
                    rep.DataBind(); 
                } 
            } 
     
            void rep_ItemCreated(object sender, RepeaterItemEventArgs e) 
            { 
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) 
                { 
                    string categoryName = (string)e.Item.DataItem; 
     
                    if (!string.IsNullOrEmpty(categoryName)) 
                    { 
                        ITextControl txt = (ITextControl)e.Item.FindControl("categoryName"); 
                        if (txt != null
                        { 
                            txt.Text = categoryName; 
                        } 
                    } 
                } 
            } 
        } 
     

    4. Register the new control in the <toolboxControls> section of the web.config to display it in Add Controls toolbox:

    <add name="NewsView Ext" section="News" type="Telerik.News.WebControls.NewsViewExtender, App_Code" /> 

    Do let us know if this helps.

    All the best,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Bob Pinna
    Bob Pinna avatar
    13 posts
    Registered:
    10 Apr 2005
    18 Dec 2008
    Link to this post
    Pepi,

    Thanks so much - works like a charm.

    Impressive product - impressive support.  You guys rock!

    ~Bob

Register for webinar
3 posts, 0 answered