1

所以我懒加载转发器控件:

下面的代码将转发器绑定到由 loadGuestbook() 填充的 guestbookData 属性

public partial class _Default
{
    private DataTable _guestbookData;
    public DataTable guestbookData
    {
        get
        {
            if (_guestbookData == null)
            {
                _guestbookData = loadGuestbook();
            }
            return _guestbookData;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    private DataTable loadGuestbook()
    {
        netguestData nd = new netguestData();
        List<post> data = nd.GetPosts(10, rptGuestbook.Items.Count);

        DataTable dt = new DataTable();
        // writing the list to a DataTable for bind happens here.
        // not sure how to cast IEnumerable to DataTable, else I'd do that

        return dt;
    }

    protected void btnLoadMore_Click(object sender, EventArgs e)
    {
        DataBind();
    }
}

使用 LINQ To SQL 从数据库中查询数据。这是我正在使用的 GetPosts(int, int) 函数:

public class netguestData
{
    private netguestEntities ne = new netguestEntities();

    public netguestData()
    {

    }

    public List<post> GetPosts(int Take, int Skip)
    {
        var posts = (from p in ne.posts
                    .OrderByDescending(p => p.postdate)
                    .Take(Take)
                    .Skip(Skip)
                    select p).ToList();
        return posts;
    }
}

现在,为了分页,我基本上每页加载 10 行,并使用转发器中的项目数作为要跳过所选数据中多少行的参考。

第一次加载页面时,我得到了最初的 10 条记录,没有任何问题,但是当我单击按钮加载下一组时,它出现空白。

调试器中的消息是:

枚举没有结果

我在点击后检查了TakeSkip值,正如预期的那样,两者都是 10。表中有超过 200 行,所以我无法理解问题所在。

谁能建议我可以做些什么来解决这个问题?

提前致谢!

4

2 回答 2

4

你可能想先跳过然后采取。(先拍摄然后跳过没有多大意义。)

于 2013-11-18T20:52:38.323 回答
1

只需通过您的查询进行推理:

    var posts = (from p in ne.posts
                .OrderByDescending(p => p.postdate)
                .Take(Take)
                .Skip(Skip)
                select p).ToList();

在第一次尝试时,您将发布 10 个帖子并跳过这 10 个帖子中的 0 个
在下一次尝试中,您将发布 10 个帖子并跳过这 10 个帖子中的 10 个,这显然没有结果。

正如它所写的那样,您总是在查询 10 个最新结果,除了第一次跳过 0 并跳过所有这些结果。

就像@Becuzz 所说的那样,您只是想交换skip 和take,这样您就可以跳过原始查询返回的结果并从其余查询中获取。

    var posts = (from p in ne.posts
                .OrderByDescending(p => p.postdate)
                .Skip(Skip)
                .Take(Take)
                select p).ToList();
于 2013-11-18T20:57:48.160 回答