自定义数据表分页 [英] Custom datatable with paging
问题描述
我用我自定义的数据表
为数据源
我的的ListView
。现在,我所面临的问题是,寻呼
是行不通的。
I am using my custom DataTable
as a DataSource
to my ListView
. Now, the problem i am facing is that the Paging
is not working.
我想要做的是,当我点击页面上的 1
, 2
等等等等,我应该能够在同一时间获取只有10行。
What i want to do is that when i click on page 1
, 2
and so on and so forth, i should be able to fetch only 10 rows at a time.
我的意思是,当页面加载我想只取前10行,就点击 2
我想获取下一个十行。但点击背页 1
不应该再次执行 SQL
语句,只是从某处(<$ C获取数据$ C>的ViewState / 缓存
)。
What i mean is that when the page loads i want to fetch only the first 10 rows, on clicking on 2
i want to fetch the next ten rows. But clicking back on page 1
should not execute the SQL
statement again, just fetch the data from somewhere(ViewState
/Cache
).
什么是正确的做法?我应该如何着手?
What is the correct approach? How should i proceed??
注意:我不想用 SQL数据源
推荐答案
您需要的是一个PagingDataSource ...
What you need is a PagingDataSource...
protected void Page_Load(object sender, EventArgs e)
{
BindData((hdnPage.Value != "" ? Convert.ToInt32(hdnPage.Value) : 0));
}
private void BindData(int? pageNo)
{
PagedDataSource ds = new PagedDataSource();
ds.DataSource = [YOUR DATA SOURCE];
ds.AllowPaging = true;
ds.PageSize = 10;
ds.CurrentPageIndex = pageNo ?? 0;
pnlPaging.Controls.Clear();
for (int i = 0; i < ds.PageCount; i++)
{
if (ds.PageCount < 2)
{
break;
}
if (pageNo == i || (pageNo == null && i == 0))
{
pnlPaging.Controls.Add(new LiteralControl("<span style=\"display:inline-block;margin:0 2px 2px 0;\">" + (i + 1).ToString() + "</span>"));
continue;
}
SuprLinkButton lb = new SuprLinkButton()
{
CommandName = "pageThis",
CommandArgument = i.ToString(),
ID = "lbPage" + i.ToString(),
Text = (i + 1).ToString()
};
lb.Attributes.Add("style", "display:inline-block;margin:0 2px 2px 0;");
lb.Command += new CommandEventHandler(lb_Command);
pnlPaging.Controls.Add(lb);
}
[LISTVIEW].DataSource = ds;
[LISTVIEW].DataBind();
}
我有一个小组来保存所有网页链接和hiddenfield保存当前页面的索引。再有就是分页命令
I have a panel to hold all the page links and a hiddenfield to save the current page index. Then there is the paging command...
protected void lb_Command(object sender, CommandEventArgs e)
{
hdnPage.Value = e.CommandArgument.ToString();
BindData(Convert.ToInt32(e.CommandArgument));
}
这篇关于自定义数据表分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!