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

[Solved] How to Save the Filter Text Value and Restore it Back to the Filter Text Box on Telerik Grid Reload

3 Answers 453 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Saurabh
Top achievements
Rank 1
Saurabh asked on 22 Aug 2013, 12:00 PM
Hello Team,

Please suggest me how to Save the Filter Text Value and Bind it Back to the Filter Text Box on Telerik Grid Reload as my requirement need to Bind the Filter text until the User Logs Off the system. I am using "2011.2.712.40" Telerik Version in my Project.

The Code I am using for my Grid is:

<div id="CmeGrid" style="padding-bottom: 5px;">
             <telerik:RadGrid ID="rdGrdInbox" runat="server" AllowFilteringByColumn="True" AllowSorting="True"
                        AllowPaging="True" CellSpacing="0" GridLines="None" AutoGenerateColumns="False"
                        PageSize="15" OnItemCommand="rdGrdInbox_ItemCommand" OnItemCreated="rdGrdInbox_ItemCreated"
                        Width="100%">
                        <MasterTableView DataKeyNames="grant_id">
                            <CommandItemSettings ExportToPdfText="Export to PDF" />
                            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                            </RowIndicatorColumn>
                            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                            </ExpandCollapseColumn>
                            <Columns>
                                <telerik:GridTemplateColumn DataField="action_required" AllowFiltering="true" SortExpression="action_required"
                                    UniqueName="action_required">
                                    <HeaderTemplate>
                                        <asp:LinkButton ID="lblheaderActionItems" runat="server" Font-Bold="true" Font-Underline="false"
                                            CommandName='Sort' CommandArgument='action_required'>Action required</asp:LinkButton>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="btnAction" runat="server" Font-Underline="true" ForeColor="Blue"
                                            Text='<%#Eval("action_required") %>' CommandName="edit" />
                                    </ItemTemplate>
                                </telerik:GridTemplateColumn>
                                <telerik:GridBoundColumn FilterControlWidth="80%" DataField="grant_display_id" HeaderText="Request ID"
                                    SortExpression="grant_display_id" UniqueName="grant_display_id" ShowFilterIcon="true">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn FilterControlWidth="80%" DataField="grant_id" HeaderText="Request ID"
                                    Visible="false" SortExpression="grant_id" UniqueName="grant_id" ShowFilterIcon="true">
                                </telerik:GridBoundColumn>
                                <telerik:GridBoundColumn FilterControlWidth="80%" DataField="activity_type" HeaderText="Type of Request"
                                    SortExpression="grant_id" UniqueName="activity_type" ShowFilterIcon="true">
                                </telerik:GridBoundColumn>
                                <telerik:GridDateTimeColumn AutoPostBackOnFilter="true" FilterControlWidth="100px"
                                    DataField="date_submitted" HeaderText="Date Submitted" SortExpression="date_submitted"
                                    UniqueName="date_submitted" PickerType="DatePicker" DataFormatString="{0:d}"
                                    FilterDelay="500" ShowFilterIcon="false">
                                </telerik:GridDateTimeColumn>
                                <telerik:GridBoundColumn FilterControlWidth="80%" DataField="grant_external_status"
                                    HeaderText="Status" SortExpression="grant_status" UniqueName="grant_status" ShowFilterIcon="true">
                                </telerik:GridBoundColumn>
                                <%--abdulr--%>
                                <telerik:GridTemplateColumn DataField="activity_title" FilterControlWidth="200px"
                                    AllowFiltering="true" SortExpression="activity_title" UniqueName="activity_title">
                                    <HeaderTemplate>
                                        <asp:LinkButton ID="lblheaderProgramTitle" runat="server" Font-Bold="true" Font-Underline="false"
                                            CommandName='Sort' CommandArgument='program_title'>Activity Title</asp:LinkButton>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="btnTitle" runat="server" Font-Underline="true" ForeColor="Blue"
                                            Text='<%#Eval("activity_title") %>' CommandName="select" />
                                    </ItemTemplate>
                                    <ItemStyle CssClass="inboxgridWrap" />
                                </telerik:GridTemplateColumn>
                                <telerik:GridNumericColumn FilterControlWidth="50px" DataField="requested_amount"
                                    HeaderText="Requested Amount" SortExpression="requested_amount" UniqueName="requested_amount"
                                    ShowFilterIcon="true" HeaderStyle-Width="90px" DataType="System.Decimal">
                                </telerik:GridNumericColumn>
                                <telerik:GridNumericColumn FilterControlWidth="50px" DataField="approved_amount"
                                    HeaderText="Approved Amount" SortExpression="approved_amount" UniqueName="approved_amount"
                                    ShowFilterIcon="true" HeaderStyle-Width="90px" DataType="System.Decimal">
                                </telerik:GridNumericColumn>
                                <telerik:GridTemplateColumn HeaderText="View/Print Agreement" SortExpression="loa_signoff_ind"
                                    AllowFiltering="false" ShowFilterIcon="false" HeaderStyle-Font-Bold="true">
                                    <ItemTemplate>
                                        <asp:LinkButton runat="server" ID="btn_view_print_agreement" Font-Underline="true"
                                            ForeColor="Blue" Text='View/Print Agreement' CommandName="viewloa"></asp:LinkButton>
                                    </ItemTemplate>
                                    <HeaderStyle Font-Bold="True" />
                                </telerik:GridTemplateColumn>
                            </Columns>
                            <EditFormSettings>
                                <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                                </EditColumn>
                            </EditFormSettings>
                        </MasterTableView>
                        <GroupingSettings CaseSensitive="false" />
                        <FilterMenu EnableImageSprites="False">
                        </FilterMenu>
                        <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
                        </HeaderContextMenu>
             </telerik:RadGrid>
</div>

Please go through the Link Below to Understand my requirement better.
LINK: http://screencast.com/t/Ip7dbtmg

Please let me know how I can save the Filter Criteria and restore on reload of Grid or if there is any inbuilt property of Telerik to do the same operation which I want. Please help me at the earliest.

Thanks,
Saurabh

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 22 Aug 2013, 01:40 PM
Hello,

<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
    AutoGenerateColumns="false" AllowFilteringByColumn="true" OnItemCommand="RadGrid1_ItemCommand" EnableLinqExpressions="false">
    <MasterTableView DataKeyNames="ID">
        <Columns>
            <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
            </telerik:GridBoundColumn>
            <telerik:GridHyperLinkColumn UniqueName="Name" DataTextField="Name" DataNavigateUrlFields="ID"
                DataNavigateUrlFormatString="~\Default.aspx?ID={0}">
            </telerik:GridHyperLinkColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
public string FilterValue
    {
        get
        {
            string text = (string)Session["FilterValue"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }
        set
        {
            Session["FilterValue"] = value;
        }
    }
 
    public System.Web.UI.Pair FilterInformation
    {
        get
        {
            if (Session["FilterInformation"] == null)
            {
                return null;
            }
            else
            {
                return (System.Web.UI.Pair)Session["FilterInformation"];
            }
        }
        set
        {
            Session["FilterInformation"] = value;
        }
    }
 
 
    
   
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        dynamic data1 = new[] {
              new { ID = 1, Name ="Name_1"},
              new { ID = 2, Name = "Name_2"},
              new { ID = 3, Name = "Name_1"},
              new { ID = 4, Name = "Name_4"},
              new { ID = 5, Name = "Name_1"}
          };
        RadGrid1.DataSource = data1;
 
        if (!Page.IsPostBack && !string.IsNullOrEmpty(FilterValue))
        {
            RadGrid1.MasterTableView.FilterExpression = "([Name] LIKE \'%" + FilterValue + "%\') ";
 
            GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("Name");
            column.CurrentFilterFunction = (GridKnownFunction)Enum.Parse(typeof(GridKnownFunction), (((System.Web.UI.Pair)FilterInformation)).First.ToString());
            column.CurrentFilterValue = FilterValue;
        }
    }
 
 
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.FilterCommandName)
        {
            if ((((System.Web.UI.Pair)(e.CommandArgument))).Second.ToString() == "Name")
            {
                if ((((System.Web.UI.Pair)(e.CommandArgument))).First.ToString() == "NoFilter")
                {
                    FilterInformation = null;
                    FilterValue = null;
                }
                else
                {
                    FilterInformation = ((System.Web.UI.Pair)(e.CommandArgument));
                    FilterValue = ((RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem)["Name"].Controls[0] as TextBox).Text;
                }
            }
        }
    }


Thanks,
Jayesh Goyani
0
Saurabh
Top achievements
Rank 1
answered on 25 Aug 2013, 04:39 AM
Hi Jayesh,

Unfortunately Yours code not working in my scenario. Please have a look to the Telerik Grid Structure I have provided and give me solution for that as the requirement I have explained in the Screencast Link.

LINK: http://screencast.com/t/Ip7dbtmg

Thanks,
Saurabh
0
Jayesh Goyani
Top achievements
Rank 2
answered on 26 Aug 2013, 08:50 AM
Hello,

Pleas check the below code snippet and screecast.

http://screencast.com/t/7w4FY8CVBhQT

Forum.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Forum.aspx.cs" Inherits="Forum"
    EnableEventValidation="false" %>
 
<%@ Register Src="~/mytruckitemcombobox.ascx" TagPrefix="uc" TagName="MyTruckItemComboBox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
    
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
 
    <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
        AutoGenerateColumns="false" AllowFilteringByColumn="true" OnItemCommand="RadGrid1_ItemCommand"
        EnableLinqExpressions="false">
        <MasterTableView DataKeyNames="ID">
            <Columns>
                <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn DataField="activity_title" FilterControlWidth="200px"
                    AllowFiltering="true" SortExpression="activity_title" UniqueName="activity_title">
                    <ItemTemplate>
                        <asp:LinkButton ID="btnTitle" runat="server" Font-Underline="true" ForeColor="Blue"
                            Text='<%#Eval("activity_title") %>' CommandName="MyCommand" />
                    </ItemTemplate>
                    <ItemStyle CssClass="inboxgridWrap" />
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
    </form>
</body>
</html>

Forum.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Telerik.Web.UI;
using System.Data;
using System.Collections;
using System.IO;
using System.Text;
using Telerik.Charting;
 
public partial class Forum : System.Web.UI.Page
{
    public string FilterValue
    {
        get
        {
            string text = (string)Session["FilterValue"];
            if (text != null)
                return text;
            else
                return string.Empty;
        }
        set
        {
            Session["FilterValue"] = value;
        }
    }
 
    public System.Web.UI.Pair FilterInformation
    {
        get
        {
            if (Session["FilterInformation"] == null)
            {
                return null;
            }
            else
            {
                return (System.Web.UI.Pair)Session["FilterInformation"];
            }
        }
        set
        {
            Session["FilterInformation"] = value;
        }
    }
 
 
     
    protected void Page_Load(object sender, System.EventArgs e)
    {
 
    }
    
 
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        dynamic data1 = new[] {
              new { ID = 1, Name ="Name_1",activity_title="title1"},
              new { ID = 2, Name = "Name_2",activity_title="title2"},
              new { ID = 3, Name = "Name_1",activity_title="title3"},
              new { ID = 4, Name = "Name_4",activity_title="title4"},
              new { ID = 5, Name = "Name_1",activity_title="title5"}
          };
        RadGrid1.DataSource = data1;
 
        if (!Page.IsPostBack && !string.IsNullOrEmpty(FilterValue))
        {
            RadGrid1.MasterTableView.FilterExpression = "([activity_title] LIKE \'%" + FilterValue + "%\') ";
 
            GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("activity_title");
            column.CurrentFilterFunction = (GridKnownFunction)Enum.Parse(typeof(GridKnownFunction), (((System.Web.UI.Pair)FilterInformation)).First.ToString());
            column.CurrentFilterValue = FilterValue;
        }
    }
 
 
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.FilterCommandName)
        {
            if ((((System.Web.UI.Pair)(e.CommandArgument))).Second.ToString() == "activity_title")
            {
                if ((((System.Web.UI.Pair)(e.CommandArgument))).First.ToString() == "NoFilter")
                {
                    FilterInformation = null;
                    FilterValue = null;
                }
                else
                {
                    FilterInformation = ((System.Web.UI.Pair)(e.CommandArgument));
                    FilterValue = ((RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem)["activity_title"].Controls[0] as TextBox).Text;
                }
            }
        }
        else if (e.CommandName == "MyCommand")
        {
            GridDataItem item = e.Item as GridDataItem;
            string strID = item.GetDataKeyValue("ID").ToString();
 
            Response.Redirect("Default2.aspx?ID=" + strID);
        }
    }
 
}


Default2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Forum.aspx">HyperLink</asp:HyperLink>
    </div>
    </form>
</body>
</html>


Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Saurabh
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Saurabh
Top achievements
Rank 1
Share this question
or