I'm using linq to sql to get my data, when I set the page size on my data grid and the user selects page 2, I get a postback and I re-read all the data to show the second page. I suspect there should be a better way of doing this, a way that ends up reading just the data I need to show. I was wondering if there are any code samples...
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
If you want really ease the database load, take a look at client-side paging...
回答2:
You should really be looking at the Skip and Take methods.
See ScottGu's post on LINQ to SQL (Part 3), and search the page for "Paging our Query Results" - this has some nice examples.
Alternatively, if you use the LinqDataSource control, and you're talking to a SQL 2005 or 2008 database, you should get this behaviour automatically. LINQ to SQL (Part 5) covers that.
回答3:
public static IEnumerable<new_log> Search(dbDataContext db, int _user,int _pageNumber, int _rowCountPerPage)
{
var query = (
from p in db.new_log
where p.created_by == (_user < 0 ? p.created_by : _user) orderby p.id descending
select p
)
.Skip(_pageNumber * _rowCountPerPage).Take(_rowCountPerPage);
return query;
}
Key Points:
1.we have to disable the viewsate in page or control level to improve the performance.
<%@ Page Language="C#" EnableViewState="false" %> OR <asp:GridView
EnableViewState="false" runat="server" />
2.Try to write single query to get result.
http://forums.asp.net/p/1779601/5120205.aspx/1?p=True&t=634814907594742030