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.Text;
using System.Data.SqlClient;
namespace zhuanjiaSystem.Fina.PageFile
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
《连接字符串》
//SqlConnection sqlConn = new SqlConnection("server=(local);uid=sa;pwd=;database=scdate");
string msg = string.Empty;
string tableName = "表名";
//页码
int PageIndex = 1;
if (Request["pageNo"] != null)
PageIndex = Int32.Parse(Request["PageNo"]);
//URL地址
string Url = "WebForm2.aspx?x=1";
if (Request["sort"] != null)
Url = string.Concat(Url,"&sort=",Request["sort"]);
//排序字段
string SortField = "唯一字段名";
if (Request["sort"] != null)
SortField = Request["sort"].ToString();
//初始化查询条
string WhereStr = "where 条件";
sqlConn.Open();
Label1.Text = bindBuyType(tableName,PageIndex,22,sqlConn,this.DataGrid1,Url,WhereStr,SortField,ref msg);
sqlConn.Close();
}
else
{
Response.Redirect("");
}
#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
public string bindBuyType(string tableName,int pageIndex,
int PageSize,
SqlConnection Conn,
DataGrid Grid1,
string Url,
string whereStr,
string SortField,
ref string msg)
{
string SqlStr = string.Concat("select count(*) from ",tableName," ",
whereStr);
//确定有多少页和多少条记录;
int RecordCount = 0;
SqlCommand Cmd = new SqlCommand(SqlStr,Conn);
SqlDataReader Reader = Cmd.ExecuteReader();
if (Reader.Read())
RecordCount = Int32.Parse(Reader[0].ToString());
Reader.Close();
Cmd.Dispose();
int PageCount = 0;
if (RecordCount % PageSize == 0)
PageCount = RecordCount/PageSize;
else
PageCount = RecordCount/PageSize + 1;
//如果是的第一页;
if (pageIndex <=1)
{
SqlStr = string.Concat("select top ",PageSize.ToString(),
" * from ",tableName," ", whereStr, " order by ",SortField," desc ");
}
//如果是最后一页;
else if (pageIndex == PageCount)
{
SqlStr = string.Concat("SELECT * FROM ( SELECT TOP ",
RecordCount - PageSize * (pageIndex-1)," * FROM ",tableName," ",
whereStr,
" ORDER BY ",SortField," ) A ORDER BY ",SortField," desc ");
}
//如果不是最后页或第一页
else
{
SqlStr = string.Concat("SELECT * FROM (SELECT TOP ", PageSize, " * FROM (",
"SELECT TOP ", PageSize*pageIndex, " * FROM ",tableName," ",
whereStr,
" ORDER BY ",SortField," desc) A ORDER BY ",SortField," ) B ",
"ORDER BY ", SortField," desc ");
}
msg = BindGrid(SqlStr,"tempTable",Grid1,Conn);
return uPageLabel(Url,PageCount.ToString(),RecordCount.ToString(),PageSize,pageIndex);
}
public string BindGrid(string strSql,
string tablename,
DataGrid grid,
SqlConnection Conn)
{
//产生数据并帮定数据
SqlDataAdapter adp=new SqlDataAdapter(strSql,Conn);
DataSet ds = new DataSet();
string message = string.Empty;
try
{
if (Conn.State == ConnectionState.Closed)
Conn.Open();
adp.Fill(ds,tablename);
grid.DataSource=ds.Tables[tablename].DefaultView;
grid.DataBind();
}
catch(Exception e)
{
message = e.Message;
}
finally
{
//释放空间资源
ds.Clear();
ds.Dispose();
adp.Dispose();
}
return message;
}
/*
* 功能 :输出分页字符串
* 参数 :Url: WEB地址;
* totalPage : 总页数;
* recordCount :总的记录数;
* perPageRec : 每页的记录数量;
* pageNo : 当前是第几页;
* 输出 :分页字符串
*/
public string uPageLabel(string Url,
string totalPage,
string recordCount,
int perPageRec,
int pageNo)
{
StringBuilder Label = new StringBuilder("");
//out label
Label.Append("<strong></strong>");
Label.Append(" 第<font color=red>").Append(pageNo).Append("</font>页/总<font color=red>").Append(totalPage).Append("</font>页|");
Label.Append("总<font color=red>").Append(recordCount).Append("</font>条|");
//the first page navigate
if (pageNo==1)
Label.Append(" 首页 | ");
else
Label.Append("<a href='").Append(Url).Append("&pageNo=1' class=whiteback>首页</a> | ");
//the provios page navigate
if (pageNo<2)
Label.Append(" 上页 | ");
else
Label.Append("<a href='").Append(Url).Append("&pageNo=").Append(pageNo-1).Append("' class=whiteback>上页</a> | ");
// then next page navigate
if (Int32.Parse(totalPage)>1 && pageNo<Int32.Parse(totalPage))
Label.Append("<a href='").Append(Url).Append("&pageNo=").Append(pageNo+1).Append("' class=whiteback>下页</a> |");
else
Label.Append("下页 |");
// then last page navigate
if (Int32.Parse(totalPage)>1 && pageNo<Int32.Parse(totalPage))
Label.Append("<a href='").Append(Url).Append("&pageNo=").Append(totalPage).Append("' class=whiteback>尾页</a> |");
else
Label.Append("尾页 |");