[英]How do I get the current page using PagedDataSource?
I am using the Umbraco CMS and trying to make next and previous buttons to scroll through a collection of pages/nodes. 我正在使用Umbraco CMS并尝试使下一个和上一个按钮滚动浏览页面/节点的集合。 I have managed to get the total number of pages to be correct but the CurrentPageIndex is always showing as 0, how do I get the CurrentPageIndex of where the page is in the list created by my PagedDataSource and increment/decrement it to move to the next page?
我设法使页面总数正确,但是CurrentPageIndex始终显示为0,如何获取页面在我的PagedDataSource创建的列表中的位置的CurrentPageIndex并递增/递减以移至下一个页?
The code I have got so far is: 到目前为止,我得到的代码是:
Asp.Net: 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#: 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();
}
Try using a <asp:ListView />
together with an <asp:DataPager />
. 尝试将
<asp:ListView />
和<asp:DataPager />
一起使用。 This simplifies paging in most situations and can do a lot of thing "automatic". 在大多数情况下,这简化了分页,并且可以“自动”执行许多操作。 See this article for a nice example.
请参阅本文 , 这是一个很好的示例。
I have used it a view times in my Umbraco projects with succes. 我在Umbraco项目中成功使用了一个查看时间。 There is also the possibility to override some standard functions of the Datapager, in case you need it.
如果需要,还可以覆盖Datapager的某些标准功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.