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;
using System.Text;namespace 自己做分页
{
public class WebForm1 : System.Web.UI.Page
{
private int PageCount;
private int nRecCount;
private int nPage;
private void Page_Load(object sender, System.EventArgs e)
{

PageDisplay();
}
private void PageDisplay()
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable(); conn.ConnectionString = "server=.;pwd=sa;uid=sa;database=你的数据库";
cmd.Connection = conn;
cmd.CommandText = "select 新闻 from 新闻表";
sda.SelectCommand = cmd;
sda.Fill(ds,"Em");
dt = ds.Tables[0];
nRecCount = dt.Rows.Count;
StringBuilder sb = new StringBuilder(""); if(nRecCount > 0)
{
PageCount = nRecCount / 10;
if(nRecCount / 10 > 0)
{
PageCount++;
}
if(this.Request.QueryString["page"] == null)
{
nPage = 1;
}
else
{

this.nPage = int.Parse(this.Request.QueryString["page"]);

}if(this.nPage < 1)
{
nPage = 1;
}
if(this.nPage > this.PageCount)
{
this.nPage = this.PageCount;
} if(nPage == 1)
{
sb.Append("<a href='WebForm1.aspx?page=1'>首页</a>")
.Append("<a href='WebForm1.aspx?page=")
.Append(nPage + 1)
.Append("'>下一页</a>")
.Append("<a href='WebForm1.aspx?page=")
.Append(PageCount)
.Append("'>尾页</a>")
.Append("&nbsp;&nbsp;&nbsp;&nbsp;页次:")
.Append(nPage.ToString())
.Append("/")
.Append(PageCount.ToString())
.Append("<br>");
}
else 
if(nPage == PageCount)
{
sb.Append("<a href='WebForm1.aspx?page=1'>首页</a>")
.Append("<a href='WebForm1.aspx?page=")
.Append(nPage - 1)
.Append("'>上一页</a>")
.Append("<a href='WebForm1.aspx?page=")
.Append(PageCount)
.Append("'>尾页</a>")
.Append("&nbsp;&nbsp;&nbsp;&nbsp;页次:")
.Append(nPage.ToString())
.Append("/")
.Append(PageCount.ToString())
.Append("<br>");
}
else
{
sb.Append("<a href='WebForm1.aspx?page=1'>首页</a>")
.Append("<a href='WebForm1.aspx?page=")
.Append(nPage - 1)
.Append("'>上一页</a>")
.Append("<a href='WebForm1.aspx?page=")
.Append(nPage + 1)
.Append("'>下一页</a>")
.Append("<a href='WebForm1.aspx?page=")
.Append(PageCount)
.Append("'>尾页</a>")
.Append("&nbsp;&nbsp;&nbsp;&nbsp;页次:")
.Append(nPage.ToString())
.Append("/")
.Append(PageCount.ToString())
.Append("<br>");
}
this.Response.Write(sb); int Start = 10 * (nPage -1);
int End = Start + 10 -1;
if(End > nRecCount -1 )
{
End = nRecCount -1;
}
this.Response.Write("<table border ='1' cellpadding = '0' cellspaceing = '0' style = 'bordercollapse:collapse'bordercolor ='#111111' bgcolor = '#ffffff'><tr>");
this.Response.Write("<td>
新闻</td></tr>"); sb.Remove(0,sb.Length); for(int i = Start; i <= End; i++)
{
sb.Append("<tr>");
for(int j = 0;j < 5;j++)
{
sb.Append("<td>" + dt.Rows[i][j].ToString() + "</td>");
}
sb.Append("</tr>");
}
sb.Append("</table>");
this.Response.Write(sb);
}
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
}
}