Repeater控件分页例子

Repeater和DataList控件提供了一个快速、灵活的表现数据的方式,但是,它们没有内建的分页功能;DataGrid控件提供了内建的分页功能,但它的结构比较复杂。下面就用PagedDataSource类实现Repeater和DataList的分页。 PagedDataSource封装了DataGrid的分页属性,我们可以象DataGrid那样进行分页。代码如下:

C#版本

<xmp>&lt;%@ Page Language="C#" %&gt; &lt;%@ import namespace="System.Data" %&gt; &lt;%@ import namespace="System.Data.OleDb" %&gt; &lt;script language="C#" runat="server"&gt; 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(); } &lt;/script&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Repeater控件分页的例子&lt;/title&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312"&gt; &lt;style&gt; P,TD,DIV,SPAN {font-size:9pt} &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;form name="form1" method="POST" runat="server"&gt; &lt;div style="padding:5px;background-color:#dedede"&gt; &lt;asp:label ID="lblCurrentPage" runat="server"&gt;&lt;/asp:label&gt;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;asp:HyperLink id="lnkPrev" runat="server"&gt;上一页&lt;/asp:HyperLink&gt; &lt;asp:HyperLink id="lnkNext" runat="server"&gt;下一页&lt;/asp:HyperLink&gt;&amp;nbsp; &lt;/div&gt; &lt;hr size="1" color="#000099"/&gt; &lt;asp:Repeater ID="Repeater1" runat="server"&gt; &lt;Itemtemplate&gt; &lt;div style="padding:5px;background-color:#dedede"&gt; &lt;%# DataBinder.Eval(Container.DataItem, "Title") %&gt; &lt;/div&gt; &lt;/Itemtemplate&gt; &lt;/asp:Repeater&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </xmp>

VB.NET版本

<xmp>&lt;%@ Page Language="VB" %&gt; &lt;%@ import namespace="System.Data" %&gt; &lt;%@ import namespace="System.Data.OleDb" %&gt; &lt;script language="VB" runat="server"&gt; Public Sub Page_Load(ByVal src As Object, ByVal e As EventArgs) Dim objConn As OleDbConnection = New OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath("../aspxWeb.mdb")) Dim objCommand As OleDbDataAdapter = New OleDbDataAdapter ("select * from Document", objConn) Dim ds As DataSet = New DataSet () objCommand.Fill(ds) Dim objPds As PagedDataSource = New PagedDataSource () objPds.DataSource = ds.Tables(0).DefaultView objPds.AllowPaging = True objPds.PageSize = 5 Dim CurPage As Integer If Not (Request.QueryString("Page") Is Nothing) Then CurPage = Convert.ToInt32(Request.QueryString("Page")) Else CurPage = 1 End If objPds.CurrentPageIndex = CurPage - 1 lblCurrentPage.Text = "当前页:" + CurPage.ToString() If Not objPds.IsFirstPage Then lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1) End If If Not objPds.IsLastPage Then lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1) End If Repeater1.DataSource = objPds Repeater1.DataBind() End Sub &lt;/script&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Repeater控件分页的例子&lt;/title&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312"&gt; &lt;style&gt; P,TD,DIV,SPAN {font-size:9pt} &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;form name="form1" method="POST" runat="server"&gt; &lt;div style="padding:5px;background-color:#dedede"&gt; &lt;asp:label ID="lblCurrentPage" runat="server"&gt;&lt;/asp:label&gt;&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;asp:HyperLink id="lnkPrev" runat="server"&gt;上一页&lt;/asp:HyperLink&gt; &lt;asp:HyperLink id="lnkNext" runat="server"&gt;下一页&lt;/asp:HyperLink&gt;&amp;nbsp; &lt;/div&gt; &lt;hr size="1" color="#000099"/&gt; &lt;asp:Repeater ID="Repeater1" runat="server"&gt; &lt;Itemtemplate&gt; &lt;div style="padding:5px;background-color:#dedede"&gt; &lt;%# DataBinder.Eval(Container.DataItem, "Title") %&gt; &lt;/div&gt; &lt;/Itemtemplate&gt; &lt;/asp:Repeater&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </xmp>

你可能感兴趣的:(Microsoft,asp.net,asp,vb,VB.NET)