不要在分页上显示所有页面

时间:2018-08-28 10:21:33

标签: c# asp.net web-applications pagination paging

我正在分页C#。我有个问题。列出时出现太多页面。 我的看法是这样的:

<<首先<上一页1 2 3 4 5 6 7 8 9 10 11 12 13下一个>最后>>

我想要这个:

<<首先<上一页... 6 7 8 9 10 ...下一页>末页>>

似乎我已经完成了大部分工作,但是我没有做这部分。

谢谢...

我的代码:

using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


public partial class product_list : System.Web.UI.Page
{
    protected int CurrentPage
    {
        get
        {
            // look for current page in ViewState
            object o = this.ViewState["_CurrentPage"];
            if (o == null)
                return 0;   // default to showing the first page
            else
                return (int)o;
        }

        set
        {
            this.ViewState["_CurrentPage"] = value;
        }
    }

    protected int PageCount
    {
        get
        {
            // look for current page count in ViewState
            object o = this.ViewState["_PageCount"];
            if (o == null)
                return 1;   // default to just 1 page
            else
                return (int)o;
        }

        set
        {
            this.ViewState["_PageCount"] = value;
        }
    }


    protected void GetItems()
    {
        string Firma = Session["FirmaID"].ToString();
        string Pryt = "01";
        string GRID = Request.QueryString["GRID"];
        lblBaslik.Text = GRID;
        string SQLGrup = "SQL";
        SqlDataAdapter da = new SqlDataAdapter(SQLGrup, bag.Bagla());
        DataTable dt = new DataTable();
        da.Fill(dt);
        bag.Bagla().Close();
        PagedDataSource pagedData = new PagedDataSource();
        DataView dv = new DataView(dt);

        pagedData.AllowPaging = true;
        pagedData.PageSize = 10;
        pagedData.DataSource = dv;
        pagedData.CurrentPageIndex = CurrentPage;


        lblCurrentPage.Text =  (CurrentPage + 1).ToString() + " / " + pagedData.PageCount.ToString();

        PageCount = pagedData.PageCount;

        // Disable Prev/Next First/Last buttons if necessary
        cmdPrev.Enabled = !pagedData.IsFirstPage;
        cmdFirst.Enabled = !pagedData.IsFirstPage;
        cmdNext.Enabled = !pagedData.IsLastPage;
        cmdLast.Enabled = !pagedData.IsLastPage;



        // Wire up the page numbers
        if (pagedData.PageCount > 1)
        {
            rptPages.Visible = true;
            ArrayList pages = new ArrayList();
            for (int i = 0; i < pagedData.PageCount; i++)
                if (i == CurrentPage)
                {

                    pages.Add("<div style='color:red; font-weight:bold'>" + (i + 1).ToString() + "</div>");
                }
                else
                {
                    pages.Add((i + 1).ToString());
                }
            rptPages.DataSource = pages;
            rptPages.DataBind();
        }
        else
        {
            rptPages.Visible = false;
        }



        rptItems.DataSource = pagedData;
        rptItems.DataBind();
    }

    protected void cmdPrev_Click(object sender, System.EventArgs e)
    {
        // Set viewstate variable to the previous page
        CurrentPage -= 1;

        // Reload control
        GetItems();
    }

    protected void cmdNext_Click(object sender, System.EventArgs e)
    {
        // Set viewstate variable to the next page
        CurrentPage += 1;

        // Reload control
        GetItems();
    }

    protected void cmdFirst_Click(object sender, System.EventArgs e)
    {
        // Set viewstate variable to the first page
        CurrentPage = 0;

        // Reload control
        GetItems();
    }
    protected void cmdLast_Click(object sender, System.EventArgs e)
    {
        // Set viewstate variable to the last page
        CurrentPage = PageCount - 1;

        // Reload control
        GetItems();
    }

    protected void rptPages_ItemCommand(object source,
                             RepeaterCommandEventArgs e)
    {
        CurrentPage = Convert.ToInt32(e.CommandArgument) - 1;
        GetItems();
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        rptPages.ItemCommand +=
           new RepeaterCommandEventHandler(rptPages_ItemCommand);
    }

    protected override void OnLoad(EventArgs e)
    {
        if (Session["Kullanici"] == null) Response.Redirect("login.aspx");

        base.OnLoad(e);

        GetItems();
    }

    protected bool CurrentPageHighlight(int currPage)
    {
        return currPage == CurrentPage ? true : false;
    }
}

我的设计代码:

<form runat="server">
<table width="100%" border="0" class="pagination">
   <tr>
      <td style="text-align:center">  <asp:label id="lblCurrentPage" runat="server"></asp:label></td>
   </tr>
   <tr>
      <td> 
<center>
                <div>
                    <div>
                        <asp:Label ID="Label1" runat="server"></asp:Label></div>
                    <div>
                        <table>
                            <tr>
                                <td>
                                    <asp:Button ID="cmdFirst" runat="server" Text="<< First" OnClick="cmdFirst_Click"></asp:Button>&nbsp;
                                </td>
                                <td>
                                    <asp:Button ID="cmdPrev" runat="server" Text="< Prev" OnClick="cmdPrev_Click"></asp:Button>&nbsp;
                                </td>
                                <td>
                                    <asp:Repeater ID="rptPages" runat="server">
                                        <HeaderTemplate>
                                            <table cellpadding="0" cellspacing="0" border="0">
                                                <tr class="text">
                                                    <td>
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:LinkButton ID="btnPage" CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server"><%# Container.DataItem %>
                                            </asp:LinkButton>&nbsp;
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            </td> </tr> </table>
                                        </FooterTemplate>
                                    </asp:Repeater>
                                </td>
                                <td>
                                    &nbsp;<asp:Button ID="cmdNext" runat="server" Text="Next>" OnClick="cmdNext_Click">
                                    </asp:Button>
                                </td>
                                <td>
                                    &nbsp;<asp:Button ID="cmdLast" runat="server" Text="Last>>" OnClick="cmdLast_Click">
                                    </asp:Button>
                                </td>
                            </tr>
                        </table>
                    </div>
                    &nbsp;</div>
            </center>      </td>
   </tr>
    <tr>
        <td>


  <div class="flex">
     <asp:Repeater ID="rptItems" runat="server">
          <HeaderTemplate>
          </HeaderTemplate>
        <ItemTemplate>
            <section>
              <img src="data:image/png;base64,<%# Eval("PIC")%>" />
              <h2><%#Eval("CODE") %></h2>
              <p><%#Eval("NAME") %></p>
              <aside>
                <ul>
                  <li>Fiyat: <%#Eval("PRICE") %> <%#Eval("UNIT") %></li>
                  <li><%#Eval("AMOUNT") %> <%#Eval("UNIT2") %></li>
                </ul>
                <button>ADD BASKET</button>
              </aside>
            </section>
        </ItemTemplate>
      </asp:Repeater>
  </div>
</form>

0 个答案:

没有答案