Last active
December 23, 2015 09:49
-
-
Save ryan-w/6616773 to your computer and use it in GitHub Desktop.
Kentico Category Filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CategoryFilter.ascx.cs" Inherits="CMSGlobalFiles_CategoryFilter" %> | |
<asp:CheckBoxList ID="Categories" runat="server" /> | |
<asp:Button ID="btnFilter" Text="Filter" OnClick="btnFilter_Click" runat="server" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web.UI.WebControls; | |
using CMS.CMSHelper; | |
using CMS.Controls; | |
using CMS.GlobalHelper; | |
using CMS.SettingsProvider; | |
using CMS.SiteProvider; | |
public partial class CMSGlobalFiles_CategoryFilter : CMSAbstractDataFilterControl | |
{ | |
protected override void OnInit(EventArgs e) | |
{ | |
SetupControl(); | |
base.OnInit(e); | |
} | |
protected void btnFilter_Click(object sender, EventArgs e) | |
{ | |
SetFilter(); | |
} | |
private void SetFilter() | |
{ | |
// Extract category ids from selected categories | |
IEnumerable<string> categoryIds = from ListItem item in Categories.Items where item.Selected select item.Value; | |
if (!categoryIds.Any()) return; // If no categoies where selected, then return | |
// Query database for selected categories | |
InfoDataSet<CategoryInfo> categories = CategoryInfoProvider.GetCategories(String.Format("CategoryID IN ({0})", String.Join(",", categoryIds)), null); | |
List<string> conditions = new List<string>(); | |
// Build a "where" condition for each one of the selected categories | |
foreach (CategoryInfo category in categories.Items) | |
conditions.Add(CategoryInfoProvider.GetCategoryDocumentsWhereCondition(category, true)); | |
// Join the where conditions with an "OR" statement to include documents that belong to any of the selected categories. | |
// You can use an "AND" statement to limit documents that belong to all of the selected categories | |
WhereCondition = conditions.Join(" OR "); | |
// Raise the filter changed event | |
RaiseOnFilterChanged(); | |
} | |
private void SetupControl() | |
{ | |
if (StopProcessing) return; | |
if (RequestHelper.IsPostBack()) return; | |
// Bind categories to check box list | |
InfoDataSet<CategoryInfo> categories = CategoryInfoProvider.GetCategories(CMSContext.CurrentSiteID); | |
Categories.DataSource = categories; | |
Categories.DataTextField = "CategoryDisplayName"; | |
Categories.DataValueField = "CategoryID"; | |
Categories.DataBind(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment