如何使用PagedDataSource获取当前页面?

时间:2013-02-13 14:00:45

标签: c# asp.net umbraco

我正在使用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();
}

1 个答案:

答案 0 :(得分:0)

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

我已经在我的Umbraco项目中使用了它的成功时间。如果需要,还可以覆盖Datapager的一些标准功能。