寻呼与GridView和LINQ到SQL [英] Paging with GridView and LINQ to SQL

查看:109
本文介绍了寻呼与GridView和LINQ到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我grideview:

My grideview:

<asp:GridView runat="server" ID="MyGridView" AutoGenerateColumns="false" 
    DataKeyNames="ID" 
    OnRowCreated="MyGridView_RowCreated" AllowPaging="true" Width="100%" 
    PageSize="5" onpageindexchanging="MyGridView_PageIndexChanging" >



我在Page_Load中后面的代码:

My code behind on page_load:

MyGridView.DataSource = new Emp.GetData();
MyGridView.DataBind();



我的代码:

My code:

using (DataContext db = new DataContext())
{
    var query = //valid query here   

    query = query.Skip(StartRowIndex *5 ).Take(5);

    return query.ToList();
}

如果我有15条记录在我的分贝,在网页加载时,我看到的链接1,2第3页为第1页数据显示 - 5条记录。然后当我去第2页5条记录,我看到第1页和第3的链接。当我去第3页我只看到2条记录,而不是5,有时分页链接不正确显示无论是。

if i have 15 records in my db, upon page load i see links for page 1,2 3 with data for page 1 shown - 5 records. then when i go to page 2 with 5 records, i see page 1 and 3 links. when i go to page 3 i see only 2 records instead of 5 and sometimes the paging link does not show up correctly either.

我要显示5条记录每页和想在GridView以确定有多少页显示。

I want to display 5 records per page and want the GridView to determine how many pages to show.

我没有使用的LinqDataSource,只是返回一个列表的方法。

i am not using a LinqDataSource, just have a method that returns a list.

推荐答案

这其实往往如果你使用的LinqDataSource

It actually tends to be easier if you do use a LinqDataSource.

<asp:LinqDataSource ID="MyDataSource" runat="server"
    OnSelecting="MyDataSource_Selecting">
</asp:LinqDataSource>



而在后台代码,你可以重新路由的LinqDataSource 来调用你的商业逻辑层。然而,现在需要离开的DataContext 对象开放,也就是说,不要在使用块把它包或者你会得到一个错误(也不会与应用手动分页跳过(..)。以(..)

And in the code-behind, you can just re-route the LinqDataSource to call your business logic layer. However, it will now need to leave the DataContext object open, i.e., don't wrap it in a using block, or you will get an error (and also don't apply the manual paging with Skip(..).Take(..).

protected void MyDataSource_Selecting(object sender,
        LinqDataSourceSelectEventArgs e) {
    e.Result = Emp.GetData();
}

现在的的LinqDataSource 应该管理所有的分页自动为您。

Now the LinqDataSource should manage all the paging for you automatically.

这篇关于寻呼与GridView和LINQ到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆