Hello,
I am using various filters in my RadGrid, including a Google-like filter, date filter, radcombobox dropdown filters, and custom radcombobox filters which include checkboxes so the users can select multiple values. All of these filters work fine....independently. The problem is that if I apply my customized filter, then attempt to apply any of the other filters (which use tableView.filter to fire the command), my custom filter values are completely overwritten.
Is there anyway to keep this from happening?
Here is a sample of my code:
Default.aspx.cs
I am on a time-crunch so any assistance is appreicated!
Thanks,
Alicia
I am using various filters in my RadGrid, including a Google-like filter, date filter, radcombobox dropdown filters, and custom radcombobox filters which include checkboxes so the users can select multiple values. All of these filters work fine....independently. The problem is that if I apply my customized filter, then attempt to apply any of the other filters (which use tableView.filter to fire the command), my custom filter values are completely overwritten.
Is there anyway to keep this from happening?
Here is a sample of my code:
Default.aspx
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AllowFilteringByColumn
=
"True"
AllowPaging
=
"True"
AllowSorting
=
"True"
CellSpacing
=
"0"
GridLines
=
"None"
Skin
=
"WebBlue"
AutoGenerateColumns
=
"True"
EnableLinqExpressions
=
"false"
OnPreRender
=
"RadGrid1_PreRender"
OnItemCommand
=
"RadGrid1_ItemCommand"
Height
=
"620px"
onneeddatasource
=
"RadGrid1_NeedDataSource"
PageSize
=
"20"
>
<
Columns
>
<
telerik:GridBoundColumn
UniqueName
=
"Date"
DataField
=
"Date"
DataType
=
"System.DateTime"
FilterControlAltText
=
"Filter Date column"
HeaderText
=
"Date"
SortExpression
=
"Date"
>
<
FilterTemplate
>
<
telerik:RadDatePicker
ID
=
"RadDatePicker1"
runat
=
"server"
ClientEvents-OnDateSelected
=
"FromDateSelected"
DbSelectedDate='<%# startDate %>'>
</
telerik:RadDatePicker
><
br
/><
br
/>
<
telerik:RadDatePicker
ID
=
"RadDatePicker2"
runat
=
"server"
ClientEvents-OnDateSelected
=
"ToDateSelected"
DbSelectedDate='<%# endDate %>'>
</
telerik:RadDatePicker
>
<
telerik:RadScriptBlock
ID
=
"RadScriptBlock2"
runat
=
"server"
>
<
script
type
=
"text/javascript"
>
function FromDateSelected(sender, args) {
var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
var ToPicker = $find('<%# ((GridItem)Container).FindControl("RadDatePicker2").ClientID %>');
var fromDate = FormatSelectedDate(sender);
var toDate = FormatSelectedDate(ToPicker);
tableView.filter("Date", fromDate + " " + toDate, "Between");
}
function ToDateSelected(sender, args) {
var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
var FromPicker = $find('<%# ((GridItem)Container).FindControl("RadDatePicker1").ClientID %>');
var fromDate = FormatSelectedDate(FromPicker);
var toDate = FormatSelectedDate(sender);
tableView.filter("Date", fromDate + " " + toDate, "Between");
}
function FormatSelectedDate(picker) {
var date = picker.get_selectedDate();
var dateInput = picker.get_dateInput();
var formattedDate = dateInput.get_dateFormatInfo().FormatDate(date, dateInput.get_displayDateFormat());
return formattedDate;
}
</
script
>
</
telerik:RadScriptBlock
>
</
FilterTemplate
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"WinLoss"
FilterControlAltText
=
"Filter WinLoss column"
HeaderText
=
"WinLoss"
SortExpression
=
"WinLoss"
UniqueName
=
"WinLoss"
>
<
FilterTemplate
>
<
telerik:RadComboBox
ID
=
"winloss_combo"
runat
=
"server"
EmptyMessage
=
"All"
AppendDataBoundItems
=
"true"
AllowCustomText
=
"true"
>
<
Items
>
<
telerik:RadComboBoxItem
runat
=
"server"
Text
=
""
Visible
=
"false"
/>
<
telerik:RadComboBoxItem
runat
=
"server"
Text
=
"Won"
/>
<
telerik:RadComboBoxItem
runat
=
"server"
Text
=
"Loss"
/>
<
telerik:RadComboBoxItem
runat
=
"server"
Text
=
"Awaiting Award"
/>
<
telerik:RadComboBoxItem
runat
=
"server"
Text
=
"No Bid"
/>
<
telerik:RadComboBoxItem
runat
=
"server"
Text
=
"Cancelled"
/>
</
Items
>
<
ItemTemplate
>
<
div
onclick
=
"StopPropagation(event)"
>
<
asp:CheckBox
ID
=
"chk1"
runat
=
"server"
onclick
=
"wlClick(this)"
/>
<
asp:Label
runat
=
"server"
ID
=
"lbl1"
AssociatedControlID
=
"chk1"
><%# Container.Text%></
asp:Label
>
</
div
>
</
ItemTemplate
>
<
FooterTemplate
>
<
hr
/>
<
asp:Button
runat
=
"server"
ID
=
"wlBtn"
Text
=
"Submit"
CommandName
=
"WinLossFilter"
/> </
FooterTemplate
>
</
telerik:RadComboBox
>
<
telerik:RadScriptBlock
ID
=
"winloss_scriptblock"
runat
=
"server"
>
<
script
type
=
"text/javascript"
>
function wlClick(chk) {
var text = "";
var values = "";
var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
var combo = $find('<%# ((GridItem)Container).FindControl("winloss_combo").ClientID %>');
//get the collection of all items
var items = combo.get_items();
//enumerate all items
for (var i = 0; i <
items.get_count
(); i++) {
var
item
=
items
.getItem(i);
var
skipEmptyItem
= i + 1;
//get the checkbox element of the current item
var chk1 = $get(combo.get_id() + "_i" + skipEmptyItem + "_chk1");
if (chk1.checked) {
text += item.get_text() + ",";
values += item.get_value() + ",";
}
}
//remove the last comma from the string
text
=
removeLastComma
(text);
values
=
removeLastComma
(values);
if (text.length > 0) {
//set the text of the combobox
combo.set_text(text);
}
else {
//all checkboxes are unchecked
//so reset the controls
combo.set_text("");
}
document.getElementById("<%= wltext.ClientID %>").value = text;
}
function removeLastComma(str) {
return str.replace(/,$/, "");
}
function StopPropagation(e) {
// Cancel bubbling.
e.cancelBubble = true;
if (e.stopPropagation) {
e.stopPropagation();
}
}
</
script
>
</
telerik:RadScriptBlock
>
</
FilterTemplate
>
</
telerik:GridBoundColumn
>
</
Columns
>
</
telerik:RadGrid
>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RadGrid1.DataBind();
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
RadGrid1.NeedDataSource += new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
protected void RadGrid1_PreRender(object sender, System.EventArgs e)
{
foreach (GridFilteringItem item in RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem))
{
//this maintains the appropriate checkboxes are checked and sets the value of the combo box
RadComboBox combo = (RadComboBox)item.FindControl("winloss_combo");
foreach (RadComboBoxItem comboItem in combo.Items)
{
if (wltext.Value.Contains(comboItem.Text.ToString()))
{
CheckBox chk = (CheckBox)comboItem.FindControl("chk1");
chk.Checked = true;
}
else
{
CheckBox chk = (CheckBox)comboItem.FindControl("chk1");
chk.Checked = false;
}
}
combo.Text = wltext.Value;
}
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
//string filterexp = RadGrid1.MasterTableView.FilterExpression;
if (e.CommandName == RadGrid.FilterCommandName)
{
Pair filterPair = (Pair)e.CommandArgument;
switch (filterPair.Second.ToString())
{
case "Date":
this.startDate = ((e.Item as GridFilteringItem)[filterPair.Second.ToString()].FindControl("RadDatePicker1") as RadDatePicker).SelectedDate;
this.endDate = ((e.Item as GridFilteringItem)[filterPair.Second.ToString()].FindControl("RadDatePicker2") as RadDatePicker).SelectedDate;
break;
default:
break;
}
}
if (e.CommandName == "WinLossFilter")
{
string query = string.Empty;
string endquery = string.Empty;
string checkedText = string.Empty;
if (wltext.Value != null)
checkedText = wltext.Value;
string str = "WinLoss," + checkedText;
if (str.Split(',').Length > 2)
{
query = "(";
endquery = ")";
}
query = query + "([WinLoss] = ";
for (int i = 1; i < str.Split(',').Length; i++)
{
String value = str.Split(',')[i];
int val = str.Split(',').Length;
query = query + "'" + value + "')";
if (i < str.Split(',').Length - 1)
{
query = query + " OR ([WinLoss] = ";
}
}
query = query + endquery;
RadGrid1.MasterTableView.FilterExpression = query ;
RadGrid1.Rebind();
}
}
I am on a time-crunch so any assistance is appreicated!
Thanks,
Alicia