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

[Solved] Caching Grid with LinqDatasource

1 Answer 118 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Iliya
Top achievements
Rank 1
Iliya asked on 12 Aug 2013, 12:14 PM
I use LinqDataSource:

<asp:LinqDataSource ID="linqDS" OnSelecting="linqDS_Selecting" runat="server" />
<telerik:RadGrid ID="gridProducts" runat="server" AllowFilteringByColumn="True" AllowPaging="True"
                  AllowSorting="True" AutoGenerateColumns="False"  AllowMultiRowSelection="false"
                  CssClass="roundBorders fixedRBPadding" PageSize="10" GridLines="None"
                 DataSourceID="linqDS" EnableAJAX="True" EnableAJAXLoadingTemplate="True">
protected void linqDS_Selecting(object sender, LinqDataSourceSelectEventArgs e)
       {
                 ...creating context...
 
                  e.Result = from productRecord in db.Products
                                 where
                                  ...
 
            
       }


I want to implement caching in grids because grids usually show only the first page and no need to make query each time.
I can easily implement caching in linqDS_Selecting method.

The problem is that i dont think about pagination in linqDS_Selecting method, grid implements it instead of me.
so i can put all data in cache (i need to save in cache only the firsts pages which can be really navigated)

I tried to figure out how to hook the event when the grid passes data about its filtering and paging to linq, but i couldnt.

Does anybody know the ways how to do it?
   

1 Answer, 1 is accepted

Sort by
0
Vasil
Telerik team
answered on 15 Aug 2013, 07:30 AM
Hello Iliya,

You can get the result during the selected event and then use it on the next selecting.

protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    e.Arguments.TotalRowCount = gridProducts.PageSize;
 
    if (Cache["MyData"] != null)
    {
        e.Result = Cache["MyData"];
    }
 
}
 
protected void LinqDataSource1_Selected(object sender, LinqDataSourceStatusEventArgs e)
{
    if (Cache["MyData"] == null && e.Result != null)
    {
        Cache["MyData"] = e.Result;
    }
}

See this forum thread for running example in the last post.

Regards,
Vasil
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
Iliya
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Share this question
or