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

[Solved] Save current filter values on postback

1 Answer 232 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 17 Jan 2010, 09:54 PM
Hi,
 I have written some code to trigger the  filtering on one button, however when I click this button the values in the filter textboxes dissappear because a postback has taken place. How do I retain the values in the filtering textboxes after posting back.

Thanks
Robert Smith

    protected void Filter()
        {
            string expression = string.Empty;
            string textFilter = string.Empty;
            string colName = string.Empty;
          
            if (this.AllowFilteringByColumn)
            {

                GridFilteringItem item = MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem;
               
              
                for (int i = 0; i < Columns.Count; i++)
                {

                    Type colType = Columns[i].DataType;
                    Telerik.Web.UI.GridColumn column = Columns[i];

                    
                    colName = MasterTableView.Columns[i].UniqueName;
                    string dataField = "";

                  
                    if (item[colName].Controls.Count <= 0)
                        continue;

                    
                    string GridColumnType = (MasterTableView.Columns[i].Owner).Columns.FindByUniqueName(colName).GetType().Name;
                    if (GridColumnType == "GridTemplateColumn")
                    {

                        GridTemplateColumn MyColumn = (GridTemplateColumn)(MasterTableView.Columns[i].Owner).Columns.FindByUniqueName(colName);
                        dataField = MyColumn.DataField;

                    }else if (GridColumnType == "GridBoundColumn" )
                    {
                        GridBoundColumn MyColumn = (GridBoundColumn)(MasterTableView.Columns[i].Owner).Columns.FindByUniqueName(colName);
                        dataField = MyColumn.DataField;
                    }
                    else if (GridColumnType == "GridCheckBoxColumn")
                    {

                        GridCheckBoxColumn MyColumn = (GridCheckBoxColumn)(MasterTableView.Columns[i].Owner).Columns.FindByUniqueName(colName);
                        dataField = MyColumn.DataField;

                    }
                 
                     if ((item[colName].Controls.Count > 2))
                    {
                        if (item[colName].Controls[1].GetType().Name == "RadComboBox")
                        {
                            textFilter = (item[colName].Controls[1] as RadComboBox).SelectedValue;
                            if (textFilter == "ALL" || textFilter == "")
                            {
                                continue;

                            }
                            else
                            {
                                if (expression != "")
                                    expression += " AND ";
                                GridDropDownColumn MyColumn = (GridDropDownColumn)(MasterTableView.Columns[i].Owner).Columns.FindByUniqueName(colName);
                                dataField = MyColumn.DataField;
                                expression += "([" + dataField + "] = " + textFilter + ")";
                                continue;

                            }

                        }
                        else
                        {
                            //at this stage we don't know what type of control it is and thus can't handle it
                            continue;

                        }

                    }


                    if (colType == typeof(System.String) && (Columns[i].Visible))
                    {

                         textFilter = (item[colName].Controls[0] as TextBox).Text;


                        if (textFilter != "")
                        {
                            if (expression != "")
                                expression += " AND ";
                            expression += "([" + dataField + "] LIKE \'%" + textFilter + "%\')";
                           
                        }
                    }

                    if ((colType == typeof(System.Int32) || colType == typeof(System.Double))&& (Columns[i].Visible))
                    {

                      
                         textFilter = (item[colName].Controls[0] as TextBox).Text;

                        if (textFilter != "")
                        {
                            if (expression != "")
                                expression += " AND ";

                            expression += "([" + dataField + "] = " + textFilter + ")";
                         
                        }

                    }

                    if (colType == typeof(System.DateTime) && (Columns[i].Visible))
                    {


                         DateTime dtFilter;
                        if ((item[colName].Controls[0] as TextBox).Text != "")
                        {
 
                          dtFilter =  Convert.ToDateTime((item[colName].Controls[0] as TextBox).Text);
                        }else
                        {
                            continue;
                        }

                        if (textFilter != null)
                        {
                            if (expression != "")
                                expression += " AND ";

                            expression += "([" + dataField + "] = \'" + dtFilter + "\')";

                        }

                        this.FilterValues[colName] = dtFilter;
                    }



                    if (colType == typeof(System.Boolean) && (Columns[i].Visible))
                    {


                        bool boolFilter = (item[colName].Controls[0] as CheckBox).Checked;

                        if (boolFilter  != false)
                        {
                            if (expression != "")
                                expression += " AND ";

                            expression += "([" + dataField + "] = " + boolFilter + ")";
                        }

                        this.FilterValues[colName] = boolFilter;
                    }

                    if (textFilter != string.Empty)
                    {
                        this.FilterValues[colName] = textFilter;
                    }

                }

             
            }
            this.MasterTableView.FilterExpression = expression;
            this.MasterTableView.Rebind();

            // Loop over the list, writing out the value

            foreach (DictionaryEntry myEntry in FilterValues)
            {
                this.Columns.FindByUniqueName(Convert.ToString(myEntry.Key)).CurrentFilterValue = Convert.ToString(myEntry.Value);

                Console.WriteLine(myEntry.Value);

            }

          
        }



1 Answer, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 21 Jan 2010, 09:22 AM
Hello Robert,

In order to retain the values in the filtering textboxes you could try before calling MasterTableView.Rebind() method to set the columns CurrentFilterValue property to the values from each filtering textbox.
You could do this by the following code snippet:
GridColumn columnID = RadGrid1.MasterTableView.GetColumnSafe("ColumnUniqueName");
columnID.CurrentFilterValue = "Value in the filtering TextBox";
 
RadGrid1.MasterTableView.Rebind();

Additionally I am sending you a simple example which illustrates how to achieve the desired scenario.

I hope this helps.

Greetings,
Radoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Mark
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Share this question
or