ASPX templates
Version 5.x > ASPX templates > Custom Filter on custom document type using aspx templates version 5.5 View modes: 
User avatar
Certified Developer v7
Certified  Developer v7
adam - 7/12/2012 5:16:43 AM
   
Custom Filter on custom document type using aspx templates version 5.5
i want to add a custom filter control to filter a cmsrepeater

looked at the documentation here http://devnet.kentico.com/docs/5_5R2/devguide/index.html?using_datasource_web_parts.htm

and also this faq - http://devnet.kentico.com/FAQs/Web-parts---Controls/Why-my-custom-filter-does-not-work-in-aspx-templat.aspx

but still can't get my datasource to filter. on inital load it loads the datasoure but when i debug, it passes the correct wherecondition and sets that to the datasource in the aspx.cs but all records are still pulled back.

here is my code for the aspx template.

<%@ Register Src="~/Controls/Filter.ascx" TagName="Filter" TagPrefix="cms" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<div id="report">
<div>
<div>
<h2><asp:Literal ID="PageName" runat="server"></asp:Literal></h2>
</div>
</div>
<article>
<cms:Filter ID="filter" runat="server" FilterName="test"/>
<cms:CMSDocumentsDataSource ID="datasource" runat="server" FilterName="test" Path="/%" ClassNames="Oxera.Publication" />
<cms:CMSRepeater ID="repeater" runat="server" FilterName="test">
<ItemTemplate>
<div class="event">
<asp:Literal ID="Topic" runat="server"></asp:Literal>
</div>
</ItemTemplate>
</cms:CMSRepeater>
</article>
<aside>

</aside>
</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="FooterPlaceHolder" Runat="Server">
</asp:Content>


here is the code behind
 protected void Page_Load(object sender, EventArgs e)
{

filter.OnFilterChanged += new CMS.Controls.ActionEventHandler(filter_OnFilterChanged);
repeater.ItemDataBound += new RepeaterItemEventHandler(AllReports_OnItemDataBound);
repeater.DataSource = datasource.DataSource;
repeater.DataBind();

}

protected void AllReports_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;

Literal topic = (Literal)e.Item.FindControl("Topic");

topic.Text = drv["Topic"].ToString();
}
}

void filter_OnFilterChanged()
{
filter.WhereCondition = filter.WhereCondition;
datasource.WhereCondition = filter.WhereCondition;
repeater.WhereCondition = datasource.WhereCondition;
repeater.ItemDataBound += new RepeaterItemEventHandler(AllReports_OnItemDataBound);
//repeater.DataSource = datasource.DataSource;
repeater.ReBind();
}


here is the code behind for my custom filter control

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMS.Controls;
using CMS.GlobalHelper;
using CMS.CMSHelper;
using CMS.PortalControls;
using CMS.SiteProvider;
using System.Data;

public partial class Controls_Filter : CMSAbstractBaseFilterControl
{
protected override void OnInit(EventArgs e)
{
SetupControl();

base.OnInit(e);
}

/// <summary>
/// PreRender event handler
/// </summary>
protected override void OnPreRender(EventArgs e)
{
// Check if the current request is a postback
if (RequestHelper.IsPostBack())
{
// Apply the filter to the displayed data
SetFilter();
}

base.OnPreRender(e);
}

private void SetupControl()
{
if (this.StopProcessing)
{
this.Visible = false;
}
else if (!RequestHelper.IsPostBack())
{
InitializeYear();
InitializeMonth();
InitializeoTopic();
}
}

private void InitializeMonth()
{
// Initialize options
this.Month.Items.Add(new ListItem("Jan", "Jan"));
this.Month.Items.Add(new ListItem("Feb", "Feb"));
this.Month.Items.Add(new ListItem("Mar", "Mar"));
}

private void InitializeYear()
{
// Initialize options
this.Year.Items.Add(new ListItem("2012", "2012"));
this.Year.Items.Add(new ListItem("2011", "2011"));
this.Year.Items.Add(new ListItem("2010", "2010"));
}

private void InitializeoTopic()
{
// Get administrator uder
UserInfo admin = UserInfoProvider.GetUserInfo(CMSContext.CurrentUser.UserID);
if (admin != null)
{
// Get custom table provider
CustomTableItemProvider ctip = new CustomTableItemProvider();
if (ctip != null)
{
// Get all topics
DataSet teams = ctip.GetItems("Oxera.Topic", null, "Topic ASC");
if (!DataHelper.DataSourceIsEmpty(teams))
{
// Bind topics to drop-down list
this.Topic.DataSource = teams.Tables[0];
this.Topic.DataTextField = "Topic";
this.Topic.DataValueField = "Topic";

this.Topic.DataBind();

}
}
}
}

/// <summary>
/// Generates WHERE condition based on current selection.
/// </summary>
private void SetFilter()
{
string where = null;

// Generate WHERE condition based on team selected
if (this.Topic.SelectedValue != null)
{
where = String.Format("Topic ='{0}'", this.Topic.SelectedValue);
}

if (where != null)
{
// Set where condition
this.WhereCondition = where;
}

this.RaiseOnFilterChanged();
}
}


Am i using the correct datasource control? is this able to work as it does using the portal template?

can i have a quick response on this please

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 7/21/2012 8:34:05 AM
   
RE:Custom Filter on custom document type using aspx templates version 5.5
Hi,

Sorry for not getting back to you earlier.
In order to have your issue resolved more quickly, we recommend to write us directly to our support email, as we cannot guarantee a timely response in the forums (it's noted under the menu).

Have you applied any hotfix to your 5.5 R2 instance? There were some issues with datasources and filters, so we it would be good to exclude this possible source of problems.

Also, have you read the article about custom filter development at devnet.kentico.com/Blogs/Karol-Jarkovsky/March-2011/Custom-filter-development-with-Kentico-CMS?

Thank you for your cooperation and patience.

Regards,
Zdenek