Custom TaxonomyControl to Custom BlogPostMasterView
We have the template of TaxonomyControl customized because we want the tags list to be rendered as links. Our plan is to place this control inside our custom BlogPostMasterView to act as a list of tags per blog post. Our TaxonomyControl and BlogPostMasterView lives in the same class library with the following namespaces:
WebsiteControls.Controls.Blogs.BlogPostMasterView
WebsiteControls.Controls.Generic.TaxonomyControl
We noticed that if we registrer the TaxonomyControl to the BlogPostMasterView the control returns an error "Error parsing the template". The line of code we for registering the control is show below:
<%@ Register TagPrefix="wc" Namespace="WebsiteControls.Controls.Generic" TagName="TaxonomyControl" Assembly="WebsiteControls" %>
Also this is the code inside the TaxonomyControl.cs:
[RequireScriptManager(true)]
public class TaxonomyControl : Telerik.Sitefinity.Web.UI.PublicControls.TaxonomyControl
private const string _layoutTemplatePath = "WebsiteControls.Controls.Generic.TaxonomyControl.TaxonomyControl.ascx";
public override string LayoutTemplatePath
get
return ConfigurationManager.AppSettings.Get("WebsiteControls_VirtualPath") + _layoutTemplatePath;
Whats wrong in here?
Hi,
Currently the Taxon filed controls used on widget templates do not output click-able links, but just plain text for the categories and tags assigned to the item. This is a feature that has not yet been implemented in the product, you can track the feature status and vote for its popularity in PITS here.
A sample workaround approach that can be implemented in the code behind of an external widget template is to extend the default template for blog posts and add a placeholder that would rener the taxonomy links for each item (in the ItemTemplate of the RadListView control displaying the blog posts). Then, in the external template's codebehind you can subscribe to the RadListView's ItemDataBound event and for each item that passes through there. process the assigned categories and render links. For example:
protected
void
Page_Load(
object
sender, EventArgs e)
NewsList.ItemDataBound +=
new
EventHandler<Telerik.Web.UI.RadListViewItemEventArgs>(NewsList_ItemDataBound);
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;
var dataItem = (NewsItem)listViewDataItem.DataItem;
if
(dataItem !=
null
)
var placeholder = e.Item.FindControl(
"CategoriesLabel"
)
as
SitefinityLabel;
placeholder.Text = RenderHierarchicalTaxaAsLink(dataItem,
"Category"
);
break
;
var placeholder = e.Item.FindControl(
"CategoriesLabel"
)
as
SitefinityLabel;
public
string
RenderHierarchicalTaxaAsLink(NewsItem newsitem,
string
taxonomyFieldName)
var catList =
""
;
var categories = newsitem.GetValue<TrackedList<Guid>>(taxonomyFieldName);
if
(categories !=
null
&& categories.Count() > 0)
catList =
"Categories:</br> "
;
var currentUrl = HttpContext.Current.Request.Url.ToString();
if
(currentUrl.EndsWith(
"/"
))
currentUrl = currentUrl.TrimEnd(
'/'
);
var taxManager = TaxonomyManager.GetManager();
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;
Thank you for your reply. At the moment we decided to just make our own control. We appreciate your help and we will keep your response for future references.