0

我正在使用 Umbraco CMS 并尝试制作下一个和上一个按钮来滚动页面/节点的集合。我已设法使总页数正确,但 CurrentPageIndex 始终显示为 0,如何获取页面在我的 PagedDataSource 创建的列表中的位置的 CurrentPageIndex 并增加/减少它以移动到下一个页?

到目前为止我得到的代码是:

ASP.NET:

<div class="detail-paging">
<div class="list-paging-control">

    <p><asp:HyperLink runat="server" id="lnkFirstPage">&lt;&lt;</asp:HyperLink></p>

    <p><asp:HyperLink runat="server" id="lnkPrev" OnServerClick="lnkPrev_Click">&lt;</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">&gt;</asp:HyperLink></p>

    <p><asp:HyperLink runat="server" id="lnkLastPage">&gt;&gt;</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();
}
4

1 回答 1

0

尝试将 a与 .<asp:ListView />一起使用<asp:DataPager />。这在大多数情况下简化了分页,并且可以做很多“自动”的事情。请参阅这篇文章以获取一个很好的示例。

我在我的 Umbraco 项目中成功地使用了它。如果您需要,也可以覆盖 Datapager 的一些标准功能。

于 2013-02-13T19:23:23.880 回答