asp.net分頁功能實現_實用技巧

來源:互聯網
上載者:User

說一下實現分頁的思路

這裡的分頁用到了一個組件 AspNetPage.dll,這個組件大家可以到網上去下載,我這裡就不提供了

添加最近到工具箱中這樣我們就可以像其他控制項一樣拖拽使用了

如圖DataPage是在工具箱中的,至於怎麼添加你們百度吧

拖拽到頁面中如圖

這個是我加完樣式後顯示的效果,怎麼樣是不是你們想要的了,如果不是你們還可以修改樣式樣式稍候奉上

先來看看要怎麼使用

 <webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="true"       PageSize="20" CssClass="paginator"  CurrentPageButtonClass="cpb"     LastPageText="尾頁" FirstPageText="首頁" PrevPageText="上一頁" NextPageText="下一頁"      UrlPaging="false" NumericButtonTextFormatString="{0}"       ShowCustomInfoSection="Left" onpagechanged="AspNetPager1_PageChanged" CustomInfoTextAlign="Left" LayoutType="Table" > </webdiyer:AspNetPager>

這個就是分頁控制項產生的程式碼

其中顯示的首頁,上一頁,下一頁,尾頁,這些都可以在屬性中定下要顯示什麼,也可以是圖片,具體的要靠你們自己去研究了

PageSize屬性設定每頁顯示的條數

UrlPageing這個屬性可以設定分頁的提交的方式,設定為true時使用url傳遞參數提交(經過自己測試這樣頁面會重新整理,所以我沒有使用url傳遞參數提交)

ShowCustomInfoSection設定顯示的的位置 有左中右三個值,至於什麼意思你懂的

onpagechanged這個事件為點擊分頁按鈕時的事件,奉上代碼

 

 //分頁事件    protected void AspNetPager1_PageChanged(object sender, EventArgs e)    {      BindView(ViewState["dataSource"] as List<Tbl_Teacher>);    }

這裡我調用了一個自訂綁定資料來源的方法BindView

 //綁定資料來源    public void BindView(List<Tbl_Teacher> ls)    {      this.AspNetPager1.RecordCount = ls.Count();      this.GridView1.DataSource = ls.Skip((AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize).Take(AspNetPager1.PageSize);      this.GridView1.DataBind();      this.AspNetPager1.CustomInfoHTML = string.Format("當前第{0}/{1}頁 共{2}條記錄 每頁{3}條", new object[]{ this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageCount, this.AspNetPager1.RecordCount, this.AspNetPager1.PageSize });    }

這裡是綁定資料來源,為了方便我使用的是linq來進行的分頁,當然這裡可以任由你來更改,可以使用預存程序,也可以傳遞直接用sql查詢,主要就兩個參數,

一個顯示的條數,一個當前的頁數,相信對於你們來說都不難事

到這裡基本上已經貼出了所有代碼,可能描述的不是很清楚,但也就這樣了,本人水平有限。下面貼上兩種樣式:

<style type="text/css">/*拍拍網風格*/.paginator { font: 11px Arial, Helvetica, sans-serif;padding:10px 20px 10px 0; margin: 0px;}.paginator a {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;margin-right:2px}.paginator a:visited {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;}.paginator .cpb {padding: 1px 6px;font-weight: bold; font-size: 13px;border:none}.paginator a:hover {color: #fff; background: #ffa501;border-color:#ffa501;text-decoration: none;}/*淘寶風格*/.paginator { font: 12px Arial, Helvetica, sans-serif;padding:10px 20px 10px 0; margin: 0px;}.paginator a {border:solid 1px #ccc;color:#0063dc;cursor:pointer;text-decoration:none;}.paginator a:visited {padding: 1px 6px; border: solid 1px #ddd; background: #fff; text-decoration: none;}.paginator .cpb {border:1px solid #F50;font-weight:700;color:#F50;background-color:#ffeee5;}.paginator a:hover {border:solid 1px #F50;color:#f60;text-decoration:none;}.paginator a,.paginator a:visited,.paginator .cpb,.paginator a:hover {float:left;height:16px;line-height:16px;min-width:10px;_width:10px;margin-right:5px;text-align:center; white-space:nowrap;font-size:12px;font-family:Arial,SimSun;padding:0 3px;} </style>

總結:這個分頁組件與資料分離,只提供了顯示頁數的功能,資料可以根據組件記錄的頁數和條數來進行綁定資料來源,還是很方便的。

如果ASP.NET實現分頁功能的描述還不夠完整,還請你們補上,大家共同學習。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.