Repeater控制項分頁的例子

來源:互聯網
上載者:User
分頁|控制項  

Repeater和DataList控制項提供了一個快速、靈活的表現資料的方式,但是,它們沒有內建的分頁功能;DataGrid控制項提供了內建的分頁功能,但它的結構比較複雜。下面就用PagedDataSource類實現Repeater和DataList的分頁。 PagedDataSource封裝了DataGrid的分頁屬性,我們可以象DataGrid那樣進行分頁。代碼如下:
 
<%@ Page Language="C#" %>
<%@ import namespace="System.Data" %>
<%@ import namespace="System.Data.OleDb" %>
<script language="C#" runat="server">
public void Page_Load(Object src,EventArgs e) {
OleDbConnection objConn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" +
Server.MapPath("../aspxWeb.mdb"));
OleDbDataAdapter objCommand=new OleDbDataAdapter("select * from Document",objConn);
DataSet ds=new DataSet();
objCommand.Fill(ds);

PagedDataSource objPds = new PagedDataSource();
objPds.DataSource = ds.Tables[0].DefaultView;
objPds.AllowPaging = true;
objPds.PageSize = 5;
int CurPage;
if (Request.QueryString["Page"] != null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage=1;

objPds.CurrentPageIndex = CurPage-1;
lblCurrentPage.Text = "當前頁:" + CurPage.ToString();

if (!objPds.IsFirstPage)
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1);

if (!objPds.IsLastPage)
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(CurPage+1);

Repeater1.DataSource=objPds;
Repeater1.DataBind();
}
</script>
<html>
<head>
<title>Repeater控制項分頁的例子</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
P,TD,DIV,SPAN {font-size:9pt}
</style>
</head>
<body>
<form name="form1" method="POST" runat="server">
<div style="padding:5px;background-color:#dedede">
<asp:label ID="lblCurrentPage" runat="server"></asp:label></td>
<td> <asp:HyperLink id="lnkPrev" runat="server">上一頁</asp:HyperLink>
<asp:HyperLink id="lnkNext" runat="server">下一頁</asp:HyperLink> 
</div>
<hr size="1" color="#000099"/>
<asp:Repeater ID="Repeater1" runat="server">
<Itemtemplate>
<div style="padding:5px;background-color:#dedede">
<%# DataBinder.Eval(Container.DataItem, "Title") %>
</div>
</Itemtemplate>
</asp:Repeater>
</form>
</body>
</html>




相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

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

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