分页程序
1
using System;2
using System.Data;3
using System.Configuration;4
using System.Collections;5
using System.Web;6
using System.Web.Security;7
using System.Web.UI;8
using System.Web.UI.WebControls;9
using System.Web.UI.WebControls.WebParts;10
using System.Web.UI.HtmlControls;11
using System.Data;12
using System.Data.SqlClient;13

14
public partial class pager : System.Web.UI.Page15


{16
string connstr = System.Configuration.ConfigurationManager.AppSettings["SQLCONNECTIONSTRING"];17
//SqlConnection myconnection = new SqlConnection(connstr);18
ArrayList Al_PageNum;19
int PageSize;20
int RecordCount;21
int PageCount;22
int CurrentPage;23
protected void Page_Load(object sender, EventArgs e)24

{25
PageSize = 5; //设定PageSize 26
SqlConnection myconnection = new SqlConnection(connstr);27
myconnection.Open();28
if (!Page.IsPostBack) //第一次请求执行 29

{30
RecordCount = CalculateRecord(); //计算总共有多少记录/31

32
PageCount = RecordCount / PageSize; //计算总共有多少页33
if (RecordCount % PageSize > 0) //取整 34
PageCount = PageCount + 1;35
lblPageCount.Text = PageCount.ToString();36
lblRecordCount.Text = RecordCount.ToString();37
ViewState["PageCount"] = PageCount;38
CurrentPage = 0;39
ViewState["PageIndex"] = 0;40

41
Al_PageNum = new ArrayList();//绑定DROPDOWNLIST42
for (int i = 1; i <= PageCount; i++) //从1开始循环,为了不出现0页码43
Al_PageNum.Add(i.ToString());44
Ddl_PageNumber.DataSource = Al_PageNum;45
Ddl_PageNumber.DataBind();46
ListBind(); //绑定 47
}48
myconnection.Close();49

50

51
}52
public int CalculateRecord() //计算总共有多少条记录53

{54
SqlConnection myconnection = new SqlConnection(connstr);55
int intCount;56
string strCount = "select count(*) as co from News";57
SqlCommand MyComm = new SqlCommand(strCount, myconnection);58
myconnection.Open();59
SqlDataReader dr = MyComm.ExecuteReader();60
if (dr.Read())61

{62
intCount = Int32.Parse(dr["co"].ToString());63
64
}65
else66

{67
intCount = 0;68
}69
dr.Close();70
myconnection.Close();71
return intCount;72
}73

74

75
ICollection CreateSource()76

{77
SqlConnection myconnection = new SqlConnection(connstr);78
int StartIndex; //设定导入的起终地址 79
StartIndex = CurrentPage * PageSize; //计算记录数的起始点80
string strSel = "select title,author,updatatime from News order by ID desc ";81
DataSet ds = new DataSet();82
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, myconnection);83
MyAdapter.Fill(ds, StartIndex, PageSize, "News");84
return ds.Tables["News"].DefaultView;85
}86

87
public void ListBind()88

{89
MyList.DataSource = CreateSource();90
MyList.DataBind();91
lbnNextPage.Enabled = true;92
lbnPrevPage.Enabled = true;93
BtnFirst.Enabled = true;94
BtnLast.Enabled = true;95
if (PageCount == 0)96

{97
lblCurrentPage.Text = "0";98
lbnNextPage.Enabled = false;99
lbnPrevPage.Enabled = false;100
BtnFirst.Enabled = false;101
BtnLast.Enabled = false;102
}103
else104

{105
if (CurrentPage == (PageCount - 1)) lbnNextPage.Enabled = false;106
if (CurrentPage == 0) lbnPrevPage.Enabled = false;107
lblCurrentPage.Text = (CurrentPage + 1).ToString();108
}109
Ddl_PageNumber.Text = lblCurrentPage.Text;110
}111

112
public void Page_OnClick(Object sender, CommandEventArgs e)113

{114
CurrentPage = (int)ViewState["PageIndex"];115
PageCount = (int)ViewState["PageCount"];116
string cmd = e.CommandName; //判断cmd,以判定翻页方向 117

118
switch (cmd)119

{120
case "next":121
if (CurrentPage < (PageCount - 1)) CurrentPage++;122
break;123
case "prev":124
if (CurrentPage > 0) CurrentPage--;125
break;126
case "Last":127
CurrentPage = (PageCount - 1);128
break;129
default:130
CurrentPage = 0;131
break;132
}133

134
ViewState["PageIndex"] = CurrentPage;135
ListBind();136
}137
public void PageNum_SelectIndexChanged(object sender, System.EventArgs e)138

{139
ViewState["PageIndex"] = int.Parse(Ddl_PageNumber.SelectedItem.Value) - 1;//保持不出现0页码140
PageSize = 5;141
CurrentPage = (int)ViewState["PageIndex"];142
PageCount = (int)ViewState["PageCount"];143
ListBind();144
//MyList.DataSource = CreateSource(); 145
//MyList.DataBind(); 146
}147

148
override protected void OnInit(EventArgs e)149

{150
InitializeComponent();151
base.OnInit(e);152
}153

154
private void InitializeComponent()155

{156
this.Load += new System.EventHandler(this.Page_Load);157
this.Ddl_PageNumber.SelectedIndexChanged += new System.EventHandler(this.PageNum_SelectIndexChanged);158
}159

160

161
162
}163

164



浙公网安备 33010602011771号