Skip to content

Instantly share code, notes, and snippets.



Last active Dec 23, 2015
What would you like to do?
Kentico Category Filter
<%@ 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" />
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)
protected void btnFilter_Click(object sender, EventArgs e)
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
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";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment