所以我懒加载转发器控件:
下面的代码将转发器绑定到由 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 条记录,没有任何问题,但是当我单击按钮加载下一组时,它出现空白。
调试器中的消息是:
枚举没有结果
我在点击后检查了Take
和Skip
值,正如预期的那样,两者都是 10。表中有超过 200 行,所以我无法理解问题所在。
谁能建议我可以做些什么来解决这个问题?
提前致谢!