Skip to content

Instantly share code, notes, and snippets.

@esitefinity
Created June 28, 2012 00:14
Show Gist options
  • Save esitefinity/3007781 to your computer and use it in GitHub Desktop.
Save esitefinity/3007781 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Sitefinity.Modules.Ecommerce.Catalog;
using Telerik.Sitefinity.Ecommerce.Catalog.Model;
using Telerik.Sitefinity.Model;
using Telerik.OpenAccess;
using Telerik.Sitefinity.Modules.Libraries;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Web.DataResolving;
namespace SitefinityWebApp.ControlTemplates
{
public partial class ProductDetailViewCustom : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var manager = CatalogManager.GetManager();
//Get the detail item URL
var itemUrl = this.GetUrlParameterString(true);
string redirectUrl = "";
//Get the actual product from the URL parameters in details page URL
var product = manager.GetItemFromUrl(typeof(Product), itemUrl, out redirectUrl) as Product;
if (product != null)
{
var assignedTags = product.GetValue<TrackedList<Guid>>("Tags");
//Here we can get any other fields either directly through the property descriptors, using GetValue
//e.g. product.GetValue<TrackedList<Guid>>("Departments");
//Or product.Price, and then query for products with similar price etc.
//Now we get the datasource for the list of products based on the above criteria
//Please note that Departments, tags etc fields of type classification are actually lists of Guids
//so you just have to check if an id is contained in the queried product's list:
var relatedSource = manager.GetProducts().ToArray().Where(p => p.GetValue<TrackedList<Guid>>("Tags").Any(t => assignedTags.Contains(t)) && p.Id != product.Id);
// So you query template should be something like
//var relatedSource = manager.GetProducts().ToArray().Where(p => p.GetValue<T>("FieldName").Contains("query criteria") && p.Id != product.Id);
//Then we bind the listview
//If no data is present, nothing will be displayed
if (relatedSource.Count() == 0)
{
MyRotator.Visible = false;
}
else
{
MyRotator.DataSource = relatedSource;
MyRotator.Visible = true;
MyRotator.ItemDataBound += new Telerik.Web.UI.RadRotatorEventHandler(MyRotator_ItemDataBound);
MyRotator.DataBind();
}
}
}
void MyRotator_ItemDataBound(object sender, Telerik.Web.UI.RadRotatorEventArgs e)
{
var item = e.Item.DataItem;
if (item != null)
{
var product = item as Product;
var tmbLnk = e.Item.FindControl("thumbnailLink") as Image;
var titleLnk = e.Item.FindControl("detailsLink") as HyperLink;
var imageLink = e.Item.FindControl("imageLink") as HyperLink;
tmbLnk.ImageUrl = product.ThumbnailUrl;
titleLnk.NavigateUrl = DataResolver.Resolve(product, "URL", null);
imageLink.NavigateUrl = DataResolver.Resolve(product, "URL", null);
titleLnk.Text = product.Title;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment