This is exactly what I need. But then I tried it. Now my radgrid does not filter at all. It is something in the prerender.. if I remove this.
Then filtering works again. Is there something that I am missing? It is like the preRender is canceling out my filter. I am using a needdatasource
https://docs.telerik.com/devtools/aspnet-ajax/knowledge-base/grid-save-and-restore-the-filters-automatically
protected void grdProducts_PreRender(object sender, EventArgs e)
{
var grid = (RadGrid)sender;
// If the flag is True
if (IsFiltering)
{
// Save the Filters
SaveFilters(grid);
// Clear the Flag by setting its value to False
IsFiltering = false;
}
// At initial Load (when not is a PostBack)
if (!IsPostBack)
{
// Restore the Filters
RestoreFilters(grid);
}
}
Can you please confirm that the code solution provided in https://docs.telerik.com/devtools/aspnet-ajax/knowledge-base/grid-save-and-restore-the-filters-automatically#solution works for you?
I just tested it and it does work on my side as you can see in the attached video.
Yes, I am getting the correct result by using the OnNeedDataSource. The sample provided in the knowledge base article binds the grid through its OnNeedDataSource property:
ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowFilteringByColumn="true" Width="800px"
OnNeedDataSource="RadGrid1_NeedDataSource"
C#
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
(sender as RadGrid).DataSource = OrdersTable();
}
Could this have something to do with it? How our columns are set up?
<telerik:GridHyperLinkColumn DataNavigateUrlFields="SupplyID" HeaderText="<%$ Code: OrderIt.Resources.LocalizedStrings.Labels_Products %>" SortExpression="Product" UniqueName="Product" DataNavigateUrlFormatString="/Pages/xxxxxx.aspx?ID={0}" DataTextField="Product" CurrentFilterFunction="Contains" AutoPostBackOnFilter="True" ShowFilterIcon="false"> </telerik:GridHyperLinkColumn>
The Hyperlink column you have is okay. I have tested it with the example from the demo and works as expected.
Here is the full code for you to test:
Markup
First <a href="https://www.google.com/">Navigate to Google</a> and then Navigate back to this page. <br /> <br /> <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" AllowFilteringByColumn="true" Width="800px" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand" OnPreRender="RadGrid1_PreRender"> <MasterTableView AutoGenerateColumns="False" DataKeyNames="OrderID"> <Columns> <telerik:GridBoundColumn DataField="OrderID" DataType="System.Int32" FilterControlAltText="Filter OrderID column" HeaderText="OrderID" ReadOnly="True" SortExpression="OrderID" UniqueName="OrderID"> </telerik:GridBoundColumn> <telerik:GridDateTimeColumn DataField="OrderDate" DataType="System.DateTime" FilterControlAltText="Filter OrderDate column" HeaderText="OrderDate" SortExpression="OrderDate" UniqueName="OrderDate"> </telerik:GridDateTimeColumn> <telerik:GridNumericColumn DataField="Freight" DataType="System.Decimal" FilterControlAltText="Filter Freight column" HeaderText="Freight" SortExpression="Freight" UniqueName="Freight"> </telerik:GridNumericColumn> <telerik:GridBoundColumn DataField="ShipName" FilterControlAltText="Filter ShipName column" HeaderText="ShipName" SortExpression="ShipName" UniqueName="ShipName"> </telerik:GridBoundColumn> <telerik:GridBoundColumn DataField="ShipCountry" FilterControlAltText="Filter ShipCountry column" HeaderText="ShipCountry" SortExpression="ShipCountry" UniqueName="ShipCountry"> </telerik:GridBoundColumn> <telerik:GridHyperLinkColumn DataNavigateUrlFields="SupplyID" HeaderText="Product" SortExpression="Product" UniqueName="Product" DataNavigateUrlFormatString="/Pages/xxxxxx.aspx?ID={0}" DataTextField="Product" CurrentFilterFunction="Contains" AutoPostBackOnFilter="True" ShowFilterIcon="false"> </telerik:GridHyperLinkColumn> </Columns> </MasterTableView> </telerik:RadGrid>
C#
// Data Binding protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) { (sender as RadGrid).DataSource = OrdersTable(); } // Create a Global Flag with its value set to False private bool IsFiltering = false; // When a Grid command is fired protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e) { // If the Command is for Filtering if (e.CommandName == RadGrid.FilterCommandName || e.CommandName == RadGrid.HeaderContextMenuFilterCommandName) { // Set the Flag's value to True IsFiltering = true; } } // Before Rendering the Grid protected void RadGrid1_PreRender(object sender, EventArgs e) { var grid = (RadGrid)sender; // If the flag is True if (IsFiltering) { // Save the Filters SaveFilters(grid); // Clear the Flag by setting its value to False IsFiltering = false; } // At initial Load (when not is a PostBack) if (!IsPostBack) { // Restore the Filters RestoreFilters(grid); } } #region GridDataSource private DataTable OrdersTable() { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("OrderID", typeof(int))); dt.Columns.Add(new DataColumn("SupplyID", typeof(int))); dt.Columns.Add(new DataColumn("OrderDate", typeof(DateTime))); dt.Columns.Add(new DataColumn("Freight", typeof(decimal))); dt.Columns.Add(new DataColumn("ShipName", typeof(string))); dt.Columns.Add(new DataColumn("Product", typeof(string))); dt.Columns.Add(new DataColumn("ShipCountry", typeof(string))); dt.PrimaryKey = new DataColumn[] { dt.Columns["OrderID"] }; for (int i = 0; i < 70; i++) { int index = i + 1; DataRow row = dt.NewRow(); row["OrderID"] = index; row["SupplyID"] = index; row["OrderDate"] = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddHours(index); row["Freight"] = index * 0.1 + index * 0.01; row["ShipName"] = "Name " + index; row["Product"] = "Product " + index; row["ShipCountry"] = "Country " + index; dt.Rows.Add(row); } return dt; } #endregion #region Save/Restore Functions private void SaveFilters(RadGrid grid) { GridFilters storedFilters = new GridFilters(); storedFilters.GridID = grid.ID; storedFilters.FilterExpression = grid.MasterTableView.FilterExpression; storedFilters.ListOfColumnFilter = new List<ColumnFilter>(); foreach (GridColumn column in grid.MasterTableView.RenderColumns.Where(x => x.SupportsFiltering())) { ColumnFilter columnFilter = new ColumnFilter(); columnFilter.ColumnName = column.UniqueName; columnFilter.CurrentFilterValue = column.CurrentFilterValue; columnFilter.CurrentFilterFunction = column.CurrentFilterFunction; columnFilter.AndCurrentFilterValue = column.AndCurrentFilterValue; columnFilter.AndCurrentFilterFunction = column.AndCurrentFilterFunction; columnFilter.ListOfFilterValues = column.ListOfFilterValues; storedFilters.ListOfColumnFilter.Add(columnFilter); } Session[grid.ID] = storedFilters; } private void RestoreFilters(RadGrid grid) { if (Session[grid.ID] == null) return; GridFilters storedFilters = Session[grid.ID] as GridFilters; if (storedFilters == null) return; grid.MasterTableView.FilterExpression = storedFilters.FilterExpression; foreach (ColumnFilter colFilter in storedFilters.ListOfColumnFilter) { GridColumn col = grid.MasterTableView.GetColumn(colFilter.ColumnName); col.CurrentFilterValue = colFilter.CurrentFilterValue; col.CurrentFilterFunction = colFilter.CurrentFilterFunction; col.AndCurrentFilterValue = colFilter.AndCurrentFilterValue; col.AndCurrentFilterFunction = colFilter.AndCurrentFilterFunction; col.ListOfFilterValues = colFilter.ListOfFilterValues; } //Session[grid.ID] = null; //Only clear the session if you're sure the filters are no longer needed. grid.Rebind(); } #endregion #region Models public class GridFilters { public string GridID { get; set; } public string FilterExpression { get; set; } public List<ColumnFilter> ListOfColumnFilter { get; set; } } public class ColumnFilter { public string ColumnName { get; set; } public string CurrentFilterValue { get; set; } public GridKnownFunction CurrentFilterFunction { get; set; } public string AndCurrentFilterValue { get; set; } public GridKnownFunction AndCurrentFilterFunction { get; set; } public string[] ListOfFilterValues { get; set; } } #endregion
The reason why it was not working for me or showing any errors was because I was using RadAjaxManager on the grid. Once I removed that I got the following error.
Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.
xxxx.Views.SupplyProducts+GridFilters' in Assembly 'xxxxx, Version=6.0.1.0, Culture=neutral, PublicKeyToken=1d62ee94bf50e8a2' is not marked as serializable.
I am storing my sessionState in SQL using SqlInMemoryProvider
Hi Mark,
I have tested my sample by enabling AJAX for the Grid using RadAjaxManager and it worked.
I think the problem you are experiencing may be related to something else. It could be an incorrect/unsupported AJAX structure. I suggest reviewing the Understanding AJAX Controls article for in-depth information about AJAX Components and how to use them. Once you get the correct AJAX settings, everything will work as expected.