The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TitlesDatesSummariesListViewCategoryLinks.ascx.cs" Inherits="SitefinityWebApp.CustomTemplates.News.Frontend.TitlesDatesSummariesListViewCategoryLinks" %>
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.ContentUI" Assembly="Telerik.Sitefinity" %>
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.Comments" Assembly="Telerik.Sitefinity" %>
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI" Assembly="Telerik.Sitefinity" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.PublicControls.BrowseAndEdit" Assembly="Telerik.Sitefinity" %>
<
telerik:RadListView
ID
=
"NewsList"
ItemPlaceholderID
=
"ItemsContainer"
runat
=
"server"
EnableEmbeddedSkins
=
"false"
EnableEmbeddedBaseStylesheet
=
"false"
>
<
LayoutTemplate
>
<
sf:ContentBrowseAndEditToolbar
ID
=
"MainBrowseAndEditToolbar"
runat
=
"server"
Mode
=
"Add"
></
sf:ContentBrowseAndEditToolbar
>
<
ul
class
=
"sfnewsList sfnewsListTitleDateSummary"
>
<
asp:PlaceHolder
ID
=
"ItemsContainer"
runat
=
"server"
/>
</
ul
>
</
LayoutTemplate
>
<
ItemTemplate
>
<
li
class
=
"sfnewsListItem"
>
<
h2
class
=
"sfnewsTitle"
>
<
sf:DetailsViewHyperLink
ID
=
"DetailsViewHyperLink1"
TextDataField
=
"Title"
ToolTipDataField
=
"Description"
runat
=
"server"
/>
</
h2
>
<
div
class
=
"sfnewsMetaInfo"
>
<
sf:FieldListView
ID
=
"PublicationDate"
runat
=
"server"
Format
=
"{PublicationDate.ToLocal():MMM dd, yyyy}"
/>
</
div
>
<
sf:FieldListView
ID
=
"summary"
runat
=
"server"
Text
=
"{0}"
Properties
=
"Summary"
WrapperTagName
=
"div"
WrapperTagCssClass
=
"sfnewsSummary"
/>
<
sf:DetailsViewHyperLink
ID
=
"FullStory"
Text="<%$ Resources:NewsResources, FullStory %>" runat="server" CssClass="sfnewsFullStory" />
<
br
/><
sf:SitefinityLabel
Id
=
"CategoriesLabel"
runat
=
"server"
></
sf:SitefinityLabel
>
<
sf:ContentBrowseAndEditToolbar
ID
=
"BrowseAndEditToolbar"
runat
=
"server"
Mode
=
"Edit,Delete,Unpublish"
></
sf:ContentBrowseAndEditToolbar
>
</
li
>
</
ItemTemplate
>
</
telerik:RadListView
>
<
sf:Pager
id
=
"pager"
runat
=
"server"
></
sf:Pager
>
<
asp:PlaceHolder
ID
=
"socialOptionsContainer"
runat
=
"server"
/>
protected
void
Page_Load(
object
sender, EventArgs e)
{
//subscribe to the ItemDataBound event of the RadListView control that displays the news items
NewsList.ItemDataBound +=
new
EventHandler<Telerik.Web.UI.RadListViewItemEventArgs>(NewsList_ItemDataBound);
}
ItemDataBound
event:void
NewsList_ItemDataBound(
object
sender, Telerik.Web.UI.RadListViewItemEventArgs e)
{
switch
(e.Item.ItemType)
{
case
RadListViewItemType.DataItem:
case
RadListViewItemType.AlternatingItem:
var listViewDataItem = (RadListViewDataItem)e.Item;
//get the actual news item that's currently bound to the ListView
var dataItem = (NewsItem)listViewDataItem.DataItem;
if
(dataItem !=
null
)
{
//get our control that displays the Categories
var placeholder = e.Item.FindControl(
"CategoriesLabel"
)
as
SitefinityLabel;
//and render the categories as clickable links
placeholder.Text = RenderHierarchicalTaxaAsLink(dataItem,
"Category"
);
}
RenderHierarchicalTaxaAsLink
we are passing the Sitefinity Content item that we resolved in the ItemDataBoundEvent and this will allow us to easily get the list of taxa assigned to it:public
string
RenderHierarchicalTaxaAsLink(NewsItem newsitem,
string
taxonomyFieldName)
{
var catList =
""
;
var itemsToTake = 10;
//get the Categories assigned to this news item
var categories = newsitem.GetValue<TrackedList<Guid>>(taxonomyFieldName).Take(itemsToTake);
itemsToTake
parameter - it controls the maximum number of categories per item that we are going to display. As resolving each category makes a call to the database, we've set that number to 10 intentionally .if
(categories !=
null
&& categories.Count() > 0)
{
catList =
"Categories:</br> "
;
//get the page URL
var currentUrl = HttpContext.Current.Request.Url.ToString();
if
(!HttpContext.Current.Request.Url.Query.IsNullOrEmpty())
currentUrl = currentUrl.Replace(HttpContext.Current.Request.Url.Query,
string
.Empty);
if
(currentUrl.EndsWith(
"/"
))
currentUrl = currentUrl.TrimEnd(
'/'
);
var taxManager = TaxonomyManager.GetManager();
//load the categories and build their filter URL to construct the clickable links
foreach
(var cat
in
categories)
{
var t = taxManager.GetTaxon<HierarchicalTaxon>(cat);
var url =
string
.Format(
"{0}/-in-{1}/{2}/{3}"
, currentUrl, t.Taxonomy.TaxonName, t.Taxonomy.Name, t.Name);
var link =
string
.Format(
"<a href='{0}'>{1}</a><br/>"
, url, t.Name).ToString();
catList = catList + link;
}
}
return
catList;
Boyan Barnev is a Principal Information Developer at Progress. His mission is to demonstrate the unlimited capabilities of Sitefinity CMS via product documentation, SDK samples, and technical blog posts. He has graduated from the American University in Bulgaria and joined Telerik in 2011. Since then Boyan has held various positions in the company, leading the strategy and operation of the Sitefinity CMS Technical support service worldwide.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.