Nathan Elg
Nathan Elg

Reputation: 187

Kentico 10 Filtered Repeater with multiple filters not working properly

I have a data source, filter, and a filtered repeater that I have created custom filters for.

For some reason, both my filters work without the other on the screen. One works fine with the other filter on the screen.

The last filter refuses to work when I have the other filter on the screen but works fine without anything else.

I am pretty sure it has something to do with my code behind files which I will put below.

FILTER#1

using CMS.DocumentEngine;
using CMS.Helpers;
using System;
using System.Web;
using System.Web.UI.WebControls;
using CMS.DocumentEngine.Web.UI;

public partial class CMSGlobalFiles_SectorFilterControl : CMSAbstractDataFilterControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    /// <summary>
    /// Sets up the inner child controls.
    /// </summary>
    private void SetupControl()
    {
        // Hides the filter if StopProcessing is enabled
        if (this.StopProcessing)
        {
            this.Visible = false;
        }
        // Initializes only if the current request is NOT a postback
        else if (!RequestHelper.IsPostBack())
        {
            // Loads product departments as filtering options
            InitializeClientSectors();
        }
    }
    /// <summary>
    /// Loads all existing product departments as filtering options into the department drop-down list.
    /// </summary>
    private void InitializeClientSectors()
    {
        // Adds the default '(all)' option
        this.drpSector.Items.Insert(0, new ListItem("(all)", "##ALL##"));
        var clientSectors = DocumentHelper.GetDocuments("BBUS.Sector")
                                .Path("/Sector/", PathTypeEnum.Children)
                                .OnSite("Balfour-dev.allata.com");
        if (!DataHelper.DataSourceIsEmpty(clientSectors))
        {
            int count = 1;
            foreach (var clientSector in clientSectors)
            {
                var ClientSectorID = clientSector.GetValue("SectorID").ToString();
                this.drpSector.Items.Insert(count++, new ListItem(clientSector.DocumentName, ClientSectorID));
            }
        }
    }
    /// <summary>
    /// Generates a WHERE condition and ORDER BY clause based on the current filtering selection.
    /// </summary>
    private void SetFilter()
    {
        string where = null;
        // Generates a WHERE condition based on the selected product department
        if (this.drpSector.SelectedIndex > 0 && this.drpSector.SelectedValue != null)
        {
            //where = string.Format("clientSector = {0}", this.drpClientSector.SelectedValue);
            where = string.Format(
                "sector LIKE '%|{0}|%' " +
                "OR sector LIKE '{0}|%' " +
                "OR sector LIKE '%|{0}' " +
                "OR sector = '{0}'", this.drpSector.SelectedValue);
        }
        if (where != null)
        {
            // Sets the Where condition
            this.WhereCondition = where;
        }
        // Raises the filter changed event
        this.RaiseOnFilterChanged();
    }
    /// <summary>
    /// Init event handler.
    /// </summary>
    protected override void OnInit(EventArgs e)
    {
        // Creates the child controls
        SetupControl();
        base.OnInit(e);
    }
    /// <summary>
    /// PreRender event handler
    /// </summary>
    protected override void OnPreRender(EventArgs e)
    {
        var ClientSectorID = HttpContext.Current.Request.QueryString.Get("SectorID");
        // Checks if the current request is a postback
        if (RequestHelper.IsPostBack())
        {
            // Applies the filter to the displayed data
            SetFilter();
        }
        else if (!string.IsNullOrEmpty(ClientSectorID))
        {
            this.drpSector.SelectedIndex = this.drpSector.Items.IndexOf(this.drpSector.Items.FindByValue(ClientSectorID));
            SetFilter();
        }
        base.OnPreRender(e);
    }
    protected void btnFilter_Click(object sender, EventArgs e)
    {
        // Remove Query Strings
        string url = Request.RawUrl.Split(new[] { '?' })[0];
        // Add ClientSectorID Query String
        string updatedQueryString = "?" + "SectorID=" + this.drpSector.SelectedValue;
        Response.Redirect(url + updatedQueryString);
    }
}

FILTER#2

using CMS.DocumentEngine;
using CMS.Helpers;
using System;
using System.Web;
using System.Web.UI.WebControls;
using CMS.DocumentEngine.Web.UI;

public partial class CMSGlobalFiles_SectorFilterControl : CMSAbstractDataFilterControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    /// <summary>
    /// Sets up the inner child controls.
    /// </summary>
    private void SetupControl()
    {
        // Hides the filter if StopProcessing is enabled
        if (this.StopProcessing)
        {
            this.Visible = false;
        }
        // Initializes only if the current request is NOT a postback
        else if (!RequestHelper.IsPostBack())
        {
            // Loads product departments as filtering options
            InitializeClientSectors();
        }
    }
    /// <summary>
    /// Loads all existing product departments as filtering options into the department drop-down list.
    /// </summary>
    private void InitializeClientSectors()
    {
        // Adds the default '(all)' option
        this.drpSector.Items.Insert(0, new ListItem("(all)", "##ALL##"));
        var clientSectors = DocumentHelper.GetDocuments("BBUS.Sector")
                                .Path("/Sector/", PathTypeEnum.Children)
                                .OnSite("Balfour-dev.allata.com");
        if (!DataHelper.DataSourceIsEmpty(clientSectors))
        {
            int count = 1;
            foreach (var clientSector in clientSectors)
            {
                var ClientSectorID = clientSector.GetValue("SectorID").ToString();
                this.drpSector.Items.Insert(count++, new ListItem(clientSector.DocumentName, ClientSectorID));
            }
        }
    }
    /// <summary>
    /// Generates a WHERE condition and ORDER BY clause based on the current filtering selection.
    /// </summary>
    private void SetFilter()
    {
        string where = null;
        // Generates a WHERE condition based on the selected product department
        if (this.drpSector.SelectedIndex > 0 && this.drpSector.SelectedValue != null)
        {
            //where = string.Format("clientSector = {0}", this.drpClientSector.SelectedValue);
            where = string.Format(
                "sector LIKE '%|{0}|%' " +
                "OR sector LIKE '{0}|%' " +
                "OR sector LIKE '%|{0}' " +
                "OR sector = '{0}'", this.drpSector.SelectedValue);
        }
        if (where != null)
        {
            // Sets the Where condition
            this.WhereCondition = where;
        }
        // Raises the filter changed event
        this.RaiseOnFilterChanged();
    }
    /// <summary>
    /// Init event handler.
    /// </summary>
    protected override void OnInit(EventArgs e)
    {
        // Creates the child controls
        SetupControl();
        base.OnInit(e);
    }
    /// <summary>
    /// PreRender event handler
    /// </summary>
    protected override void OnPreRender(EventArgs e)
    {
        var ClientSectorID = HttpContext.Current.Request.QueryString.Get("SectorID");
        // Checks if the current request is a postback
        if (RequestHelper.IsPostBack())
        {
            // Applies the filter to the displayed data
            SetFilter();
        }
        else if (!string.IsNullOrEmpty(ClientSectorID))
        {
            this.drpSector.SelectedIndex = this.drpSector.Items.IndexOf(this.drpSector.Items.FindByValue(ClientSectorID));
            SetFilter();
        }
        base.OnPreRender(e);
    }
    protected void btnFilter_Click(object sender, EventArgs e)
    {
        // Remove Query Strings
        string url = Request.RawUrl.Split(new[] { '?' })[0];
        // Add ClientSectorID Query String
        string updatedQueryString = "?" + "SectorID=" + this.drpSector.SelectedValue;
        Response.Redirect(url + updatedQueryString);
    }
}

Upvotes: 1

Views: 386

Answers (1)

Trevor F
Trevor F

Reputation: 1437

Sadly this is a little bit of a limitation, you can only have 1 filter per Repeater/Data Source (except Smart Search it seems, which can handle multiple).

You will most likely need to combine both of your filters into 1 filter, and combine the logics into one where condition.

https://docs.kentico.com/k10/developing-websites/loading-and-displaying-data-on-websites/filtering-and-paging-data

Would be great to allow multiple filters though!

Upvotes: 1

Related Questions