Pre 5.4 code example on using choice field to filter grid in FrontEnd UserControl
Hello everybody
1. All credit to this post go to Jen Peleva.
2. I think it might be very common that you create a module in module builder and have a choice field which you want to use later to filter the results.
Please note that pre 5.3 it seems choicefields were of type string[] and later ChoiceOption so this code is for pre 5.4 modules. We wasted some time because I importat that module to an 5.4 version.
The idea is the following in my example.
You have employees (German Mitarbeiter) and want to filter them by function (German Funktion)
See the working example here on this demo site: schoefflisdorf.ch.mserver2.arvixevps.com/.../personenregister
Well I have an ascx which has the following code:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="mitarbeiter_liste.ascx.cs" Inherits="SitefinityWebApp.UserControls.mitarbeiter_liste" %>
<
br
/>
<
table
>
<
tr
><
td
>Filtern Sie hier: </
td
><
td
><
telerik:RadComboBox
ID
=
"RadComboBox2"
runat
=
"server"
AutoPostBack
=
"true"
Width
=
"250px"
OnDataBound
=
"RadComboBox2_DataBound"
>
</
telerik:RadComboBox
></
td
></
tr
></
table
>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"False"
CellSpacing
=
"0"
GridLines
=
"None"
AllowSorting
=
"True"
ShowGroupPanel
=
"False"
Skin
=
"Metro"
onneeddatasource
=
"RadGrid1_NeedDataSource"
>
<
ClientSettings
AllowDragToGroup
=
"True"
>
</
ClientSettings
>
<
MasterTableView
>
<
CommandItemSettings
ExportToPdfText
=
"Export to PDF"
></
CommandItemSettings
>
<
RowIndicatorColumn
Visible
=
"True"
FilterControlAltText
=
"Filter RowIndicator column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
RowIndicatorColumn
>
<
ExpandCollapseColumn
Visible
=
"True"
FilterControlAltText
=
"Filter ExpandColumn column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
ExpandCollapseColumn
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"Nachname"
FilterControlAltText
=
"Nachname"
HeaderText
=
"Nachname"
UniqueName
=
"Nachname"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"Vorname"
FilterControlAltText
=
"Vorname"
HeaderText
=
"Vorname"
UniqueName
=
"Vorname"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"Telefon"
FilterControlAltText
=
"Telefon"
HeaderText
=
"Telefon"
UniqueName
=
"Telefon"
>
</
telerik:GridBoundColumn
>
<%-- <
telerik:GridBoundColumn
DataField
=
"Mail"
FilterControlAltText
=
"Email"
HeaderText
=
"E-Mail"
UniqueName
=
"Email"
>
</
telerik:GridBoundColumn
>--%>
<
telerik:GridHyperLinkColumn
FilterControlAltText
=
"Email"
HeaderText
=
"E-Mail"
UniqueName
=
"Email"
DataNavigateUrlFormatString
=
"mailto:0"
DataTextField
=
"Mail"
DataNavigateUrlFields
=
"Mail"
Target
=
"_blank"
></
telerik:GridHyperLinkColumn
>
<
telerik:GridBoundColumn
DataField
=
"Taetigkeit"
FilterControlAltText
=
"Taetigkeit"
HeaderText
=
"Tätigkeit"
UniqueName
=
"Taetigkeit"
>
</
telerik:GridBoundColumn
>
</
Columns
>
<
EditFormSettings
>
<
EditColumn
FilterControlAltText
=
"Filter EditCommandColumn column"
></
EditColumn
>
</
EditFormSettings
>
</
MasterTableView
>
<
FilterMenu
EnableImageSprites
=
"False"
></
FilterMenu
>
</
telerik:RadGrid
>
public
void
RetrieveCollectionOfFunktionen()
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
Type mitarbeiterType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.Mitarbeiter.Mitarbeiter"
);
List<DynamicContent> dataItems = dynamicModuleManager.GetDataItems(mitarbeiterType).Where(s => s.Status == ContentLifecycleStatus.Live).ToList();
List<
string
> funktionDropdownDataSource =
new
List<
string
>();
foreach
(var dataItem
in
dataItems)
var funktionValue = dataItem.GetValue<
string
[]>(
"Funktion"
);
funktionDropdownDataSource.AddRange(funktionValue);
//order the results ASC (my choiceFiled name si "Fruits")
// var dataSourceASC = funktionDropdownDataSource.OrderBy(x => x.FieldValue<string>("Funktion"));
var dataSourceASC = funktionDropdownDataSource.OrderBy(x => x);
//distinct the results
var distinctSelection = dataSourceASC.Distinct();
RadComboBox2.DataSource = distinctSelection;
RadComboBox2.DataBind();
protected
void
RadComboBox2_DataBound(
object
sender, EventArgs e)
var combo = (RadComboBox)sender;
combo.Items.Insert(0,
new
RadComboBoxItem(
"Alle Einträge"
,
string
.Empty));
protected
void
Page_Load(
object
sender, EventArgs e)
//we are getting kategorien für filter
RetrieveCollectionOfFunktionen();
if
(!Page.IsPostBack)
// Fetch a collection of "live" and "visible" mitarbeiter items.
var myCollection = GetDataItems();
// Binds the collection of Person items to the RadGrid
RadGrid1.DataSource = myCollection;
RadGrid1.DataBind();
else
// Fetch a collection of "live" and "visible" mitarbeiter items.
var myCollection = GetDataItemsFiltered();
// Binds the collection of Person items to the RadGrid
RadGrid1.DataSource = myCollection;
RadGrid1.DataBind();
// Gets a collection of "live" and "visible" mitarbeiter items.
public
IQueryable<DynamicContent> GetDataItems()
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
Type mitarbeiterType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.Mitarbeiter.Mitarbeiter"
);
// Fetch a collection of "live" and "visible" mitarbeiter items.
var myCollection = dynamicModuleManager.GetDataItems(mitarbeiterType).Where(i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible ==
true
).OrderBy(i => i.FieldValue<
string
>(
"Nachname"
));
return
myCollection;
// Gets a filtered collection of "live" and "visible" mitarbeiter items.
public
IQueryable<DynamicContent> GetDataItemsFiltered()
DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
Type mitarbeiterType = TypeResolutionService.ResolveType(
"Telerik.Sitefinity.DynamicTypes.Model.Mitarbeiter.Mitarbeiter"
);
// Fetch a collection of "live" and "visible" mitarbeiter items.
var myCollection = dynamicModuleManager.GetDataItems(mitarbeiterType).Where((i => i.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && i.Visible ==
true
&& i.FieldValue<
string
[]>(
"Funktion"
).Contains(RadComboBox2.SelectedValue.ToString()))).OrderBy(i => i.FieldValue<
string
>(
"Nachname"
));
if
(myCollection.Count() == 0)
var myNewCollection = GetDataItems();
return
myNewCollection;
else
return
myCollection;
protected
void
RadGrid1_NeedDataSource(
object
sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
// Fetch a collection of "live" and "visible" mitarbeiter items.
var myCollection = GetDataItems();
// Binds the collection of Person items to the RadGrid
RadGrid1.DataSource = myCollection;
Hi Markus,
Thank you for sharing the example with the community. I am sure that it will be useful to a lot of users.
All the best,
Victor Velev
the Telerik team
Dear Victor
As mentioned
a) credit goes to Jen from telerik
b) I think this is very common. I would love to see this as a KB article for pre 5.4 and 5.4 + code with link in the module builder code reference.
Markus
Hello Makrus,
We will consider creating an article about the topic as soon as possible.
Thank you for feedback!
Regards,
Victor Velev
the Telerik team