<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="test1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<table><asp:Repeater id="Repeater1" runat="server">
<ItemTemplate>
<tr><td><%# DataBinder.Eval(Container.DataItem,"orderid") %></td><td><%# DataBinder.Eval(Container.DataItem,"shipname") %></td></tr>
</ItemTemplate>
</asp:Repeater></table> </FONT>
<br>
<asp:Label id="Label1" runat="server">第几页</asp:Label>
<asp:HyperLink id="HyperLink1" runat="server">首页</asp:HyperLink>
<asp:HyperLink id="Hyperlink2" runat="server">上一页</asp:HyperLink>
<asp:HyperLink id="Hyperlink3" runat="server">下一页</asp:HyperLink>
<asp:HyperLink id="Hyperlink4" runat="server">尾页</asp:HyperLink>
<asp:Label id="Label2" runat="server">共几页,几条</asp:Label>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace test1
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.HyperLink Hyperlink2;
protected System.Web.UI.WebControls.HyperLink Hyperlink3;
protected System.Web.UI.WebControls.HyperLink Hyperlink4;
protected System.Web.UI.WebControls.Repeater Repeater1;
//protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
SqlConnection conn=new SqlConnection("server=127.0.0.1;database=Northwind;uid=sa;pwd=;");
SqlDataAdapter cmd=new SqlDataAdapter("select * from [orders] order by orderid desc",conn);
DataSet ds=new DataSet();
cmd.Fill(ds);
PagedDataSource objPds=new PagedDataSource();
objPds.DataSource=ds.Tables[0].DefaultView;
objPds.AllowPaging=true;
//string recordcount=objPds.DataSourceCount.ToString();//objPds.Count.ToString(); //记录总数
objPds.PageSize=5;
string pagecount=objPds.PageCount.ToString(); //总页数
//Label2.Text="(共"+pagecount+"页,";
//Label3.Text=recordcount+"条)";
int CurPage;
if (Request.QueryString["Page"] != null)
{
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
}
else
{
CurPage=1;
}
objPds.CurrentPageIndex = CurPage-1;
//Label1.Text = "第" + CurPage.ToString() + "页";
if (!objPds.IsFirstPage)
{
Hyperlink2.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1);
}
if(!objPds.IsLastPage)
{
Hyperlink3.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage+1);
}
if(!objPds.IsFirstPage)
{
HyperLink1.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=1";
}
if(!objPds.IsLastPage)
{
Hyperlink4.NavigateUrl=Request.CurrentExecutionFilePath + "?Page="+pagecount;
}
Repeater1.DataSource=objPds;
Repeater1.DataBind();
conn.Close();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}