Display Category Name Only
New to Telerik and .Net in general but I have a couple of questions I'm hoping to get some help on. I'm trying to use the category name of an event as part of a URL that I'm trying to structure within a custom widget.
Example:
I have spent a lot of time looking for the answer online of how to simply display the category name but have only come up with the following code:
<sitefinity:HierarchicalTaxonField ID="HierarchicalFieldControl" runat="server" TaxonomyId="E5CD6D69-1543-427b-AD62-688A99F5E7D4" DisplayMode="Read" WebServiceUrl="~/Sitefinity/Services/Taxonomies/HierarchicalTaxon.svc" Expanded="false" TaxonomyMetafieldName="Category" BindOnServer="true" />
And while it does display the category name it also displays a bunch of html along with it that I don't want. I simply want a string of the name itself.
While I have some experience with c# and .Net I don't have any experience doing any kind of code behind in Sitefinity or custom whatevers and have been writing any c# inline within the widgets and has worked well for now. If I'm able to do that with displaying the category name it would be most helpful.
Hi Jesse,
Can you please share some additional information with us regarding where you'd like to display the category names so we can make sure we're addressing the same issue.
Do you simply need to display a list of each category that's assigned to an item in the list/details view of one of our ContentView widgets (e.g. Blogs) or do you need to have filtering ability for these names as well?
By default the Hierarchical(or Flat)taxonField controls will resolve the taxa an item has been marked with, and display their titles if the control is placed on the widget template.
However 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;
yeah, this seems to be more work than its worth. All i want to do is to convert a single tag that would be attached to an item and display that tag as a string so I can have it be part of a URL like so:
I appreciate the help though. If you know of an easier way to accomplish this outside of getting into the code behind I'd appreciate hearing it(or in this case reading it)
Jesse, did you ever find a solution to this? I'm trying to figure out the same thing and I came across this post.