This is a migrated thread and some comments may be shown as answers.

Custom FilterTemplate expressions being overwritten by non-custom filters

3 Answers 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alicia
Top achievements
Rank 1
Alicia asked on 14 Jul 2011, 03:08 PM
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 
  
<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

3 Answers, 1 is accepted

Sort by
0
Alicia
Top achievements
Rank 1
answered on 18 Jul 2011, 01:51 PM
Can anyone shed some light on this?  As stated before, I am pressed for time and would be very grateful for a quick response!

Thank you!!
Alicia
0
Alicia
Top achievements
Rank 1
answered on 19 Jul 2011, 02:55 PM
Anyone??
0
Vasil
Telerik team
answered on 20 Jul 2011, 10:58 AM
Hello Alicia,

This happens because the grid create new FilterExpression every time depending on the filtering settings. Thus your custom filtering is not build-in for the grid, you should care manually to apply it every time.

For example you could store your filtering expression in the session and apply it on every filtering.
protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.FilterCommandName)
    {
           .......
    }
    if (e.CommandName == "WinLossFilter")
    {
       ..........
          
        query = query + endquery;
        Session["MyFilterExpression"] = query;
    }
     
 
    if (Session["MyFilterExpression"] != null)
    {
        e.Canceled = true;
        RadGrid1.MasterTableView.FilterExpression += Session["MyFilterExpression"].ToString();
        RadGrid1.Rebind();
    }
     
  
}

Additionally you could see this help topic:
http://www.telerik.com/help/aspnet-ajax/grid-custom-option-for-filtering.html

Greetings,
Vasil
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
Grid
Asked by
Alicia
Top achievements
Rank 1
Answers by
Alicia
Top achievements
Rank 1
Vasil
Telerik team
Share this question
or