如何使用PagedDataSource获取当前页面
本文关键字:当前页 获取 PagedDataSource 何使用 | 更新日期: 2023-09-27 17:58:53
我正在使用Umbraco CMS,并试图制作下一个和上一个按钮来滚动页面/节点的集合。我已经设法使页面总数正确,但CurrentPageIndex总是显示为0,我如何获得页面在由我的PagedDataSource创建的列表中的CurrentPageIndex,并递增/递减以移动到下一页?
到目前为止,我得到的代码是:
Asp.Net:
<div class="detail-paging">
<div class="list-paging-control">
<p><asp:HyperLink runat="server" id="lnkFirstPage"><<</asp:HyperLink></p>
<p><asp:HyperLink runat="server" id="lnkPrev" OnServerClick="lnkPrev_Click"><</asp:HyperLink></p>
</div>
<div class="list-paging-numbers">
<p><asp:Label runat="server" ID="lblCurrentScroll"></asp:Label></p>
</div>
<div class="list-paging-control left-marker">
<p><asp:HyperLink runat="server" id="lnkNext" OnServerClick="lnkNext_Click">></asp:HyperLink></p>
<p><asp:HyperLink runat="server" id="lnkLastPage">>></asp:HyperLink></p>
</div>
<div class="list-paging-text">
<p><asp:HyperLink runat="server" ID="lnkViewAll" OnClick="ViewAll_Click">View All</asp:HyperLink></p>
</div>
C#:
private PagedDataSource paging;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DoDataBind();
}
}
public int PageNumber
{
get
{
if (ViewState["PageNumber"] != null)
return Convert.ToInt32(ViewState["PageNumber"]);
else
return 0;
}
set
{
ViewState["PageNumber"] = value;
}
}
//ALL CODE THAT BINDS DATA FROM UMBRACO
public void DoDataBind()
{
//Get the parent node.
Node parent = new Node(1152);
//Get all the BrandsDetailPage children and order alphabetically.
IEnumerable<INode> nodes = HelperMethods.GetChildrenOfType(parent, "BrandsDetailPage", true, true).OrderBy(n => n.Name);
//Pagination
paging = new PagedDataSource()
{
AllowPaging = true,
DataSource = nodes.ToList(),
CurrentPageIndex = PageNumber,
PageSize = 1
};
//pageNumber = paging.CurrentPageIndex;
CreateNavigation();
}
//ALL CODE THAT BINDS DATA FROM UMBRACO
//PAGINATION
public void CreateNavigation()
{
lblCurrentScroll.Text = PageNumber.ToString();
lblCurrentScroll.Text += " of ";
lblCurrentScroll.Text += paging.PageCount;
Node node = new Node(1153);
string nodeUrl = node.Url;
lnkViewAll.NavigateUrl = string.Format("{0}?page=All", nodeUrl);
}
protected void lnkPrev_Click(object sender, EventArgs e)
{
PageNumber--;
DoDataBind();
}
protected void lnkNext_Click(object sender, EventArgs e)
{
PageNumber++;
DoDataBind();
}
尝试将<asp:ListView />
与<asp:DataPager />
一起使用。这在大多数情况下简化了分页,并且可以"自动"完成很多事情。请参阅本文中的一个好例子。
我在我的Umbraco项目中多次使用它,并取得了成功。如果您需要的话,也可以覆盖Datapager的一些标准功能。