分页代码问题

时间:2015-07-02 05:34:26

标签: c# asp.net c#-4.0 pagination

我已经编写了以下代码行,用于使用转发器控件在asp.net中实现分页

  <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" OnClick="pagenexte_Click"
    data-rel="tooltip" data-original-title="previous page.">&laquo;</asp:LinkButton>
<asp:Repeater ID="rptPager" OnItemDataBound="rptPager_ItemDataBound" runat="server">
    <ItemTemplate>
        <asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>' Enabled='<%# Eval("Enabled") %>' OnClick="Page_Changed"></asp:LinkButton>
    </ItemTemplate>
</asp:Repeater>
<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="false" OnClick="pagenext_Click" 
       data-rel="tooltip" data-original-title="next page.">&raquo;
</asp:LinkButton>

<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="false" OnClick="imgnext_Click"
    data-rel="tooltip" data-original-title="next page." Visible="false">    &raquo;
</asp:LinkButton>

在cs文件中

    public void UpdatePageLables(int aPageCount)
{
    if (!Page.IsPostBack)
    {
        Session["pages"] = null;
    }
    PageCount = (int)Math.Ceiling((decimal)aPageCount / PageSize);
    int recordCount = PageCount;
    if (PageSizeChanged != null)
    {
        HiddenField hd = new HiddenField();

        int current;
        current = PageIndex;
        int pre;
        int Next;
        double dblPageCount = (double)((decimal)recordCount / decimal.Parse(lstPageSize.SelectedValue));
        int pageCount = PageCount;

        List<ListItem> pages = new List<ListItem>();
        if (pageCount > 0)
        {
            // pages.Add(new ListItem("First", "1", PageIndex > 1));
            current = PageIndex;
            pre = --PageIndex;
            PageIndex = current;

            // pages.Add(new ListItem("Previous", pre.ToString(), PageIndex > 1));

            for (int i = 1; i <= aPageCount; i++)
            {
                if (i <= 5)
                {
                    pages.Add(new ListItem(i.ToString(), i.ToString(), i != PageIndex));
                }
            }


            int currentPage = PageIndex;
            Next = ++PageIndex;
            PageIndex = currentPage;
            //pages.Add(new ListItem("Next", Next.ToString(), PageIndex < pageCount));
            // pages.Add(new ListItem("Last", pageCount.ToString(), PageIndex < pageCount));

            hd.Value = (pre.ToString());


        }
        if (Session["pages"] != null)
        {
            rptPager.DataSource = Session["pages"];
            rptPager.DataBind();
        }
        else
        {
            rptPager.DataSource = pages;
            rptPager.DataBind();
        }

        Session["lastnumber"] = 5;
        Session["pagecount"] = aPageCount;
        Session["orignalpages"] = pages;

    }

}
protected void pagenext_Click(object sender, EventArgs e)
{
    List<ListItem> pages = new List<ListItem>();
    int pagecount = Convert.ToInt32(Session["pagecount"].ToString());
    int lastnumber = Convert.ToInt32(Session["lastnumber"].ToString());
    if (lastnumber > pagecount)
    {
        Session["lastnumber"] = 1;
        lastnumber = 1;
        Session["pagecount"] = pagecount;
        Session["orignalpages"] = pages;

    }


    int limit = lastnumber + 5;
    for (int i = lastnumber; i <= pagecount; i++)
    {
        if (i <= limit)
        {
            pages.Add(new ListItem(i.ToString(), i.ToString(), i != PageIndex));
        }
        else
        {
            rptPager.DataSource = Session["orignalpages"];
            rptPager.DataBind();
        }
    }


    Session["lastnumber"] = limit;
    Session["pagecount"] = pagecount;

    Session["pages"] = pages;

    rptPager.DataSource = pages;
    rptPager.DataBind();

}

protected void pagenexte_Click(object sender, EventArgs e)
{

    List<ListItem> pages = new List<ListItem>();
    int pagecount = Convert.ToInt32(Session["pagecount"].ToString());
    int lastnumber = Convert.ToInt32(Session["lastnumber"].ToString());
    if (lastnumber > pagecount)
    {
        Session["lastnumber"] = pagecount - 5;
        lastnumber = pagecount - 5;
        Session["pagecount"] = pagecount;
        Session["orignalpages"] = pages;

    }
    else
    {

        Session["lastnumber"] = 1;
        lastnumber = 1;
        Session["pagecount"] = pagecount;
        Session["orignalpages"] = pages;
    }

    int limit = lastnumber + 5;
    for (int i = lastnumber; i <= pagecount; i++)
    {
        if (i <= limit && i>0) 
        {
            pages.Add(new ListItem(i.ToString(), i.ToString(), i != PageIndex));
        }
        else
        {
            //rptPager.DataSource = Session["orignalpages"];
            //rptPager.DataBind();
        }
    }


    Session["lastnumber"] = limit;
    Session["pagecount"] = pagecount;

    Session["pages"] = pages;

    rptPager.DataSource = pages;
    rptPager.DataBind();

}

它工作正常,但不会根据需要显示页码。 基本上我想它应该显示为“1 2 3 4 5”然后点击下一个按钮,它应显示“6 7 8 9 10”然后再次点击下一步按钮“11 12 13 14 15”....等等。以下代码生成页码为 “1 2 3 4 5”点击下一步按钮“5 6 7 8 9 10”然后10 11 12 13 14 15等等......请帮助我!!!还帮我改进以前按钮的代码 也...

1 个答案:

答案 0 :(得分:0)

您可以使用“DataTable”javascript功能来解决此问题。请参阅https://www.datatables.net/

在此您只需要在下面的代码中提供您的表格。

$(document).ready(function(){
    $('#myTable').DataTable();
});