asp.net分页功能的实现

效果图:
 


代码:
static int PageSize = 30;

#region 分页部分代码 //*********************************** 数据分页 ******************************** protected void FirstPage_Click1(object sender, EventArgs e) { int RecordCount = GetRecordCount(ViewState["ssearchCondition"].ToString()); //获取记录条数 DataTable dt = GetListByPage(ViewState["ssearchCondition"].ToString(),1,PageSize); int PageCount = RecordCount % PageSize > 0 ? RecordCount / PageSize + 1 : RecordCount / PageSize; int CurrentPageNum; if (PageCount == 0) { CurrentPageNum = 0; } else { CurrentPageNum = 1; } CurrentPage.Text = (CurrentPageNum).ToString(); TotalPageCount.Text = PageCount.ToString(); EnableJudger(); ViewState["PageCount"] = PageCount; ViewState["CurrentPageNum"] = CurrentPageNum; int rowNum = 1; if (int.Parse(ViewState["PageCount"].ToString()) > 0) { rowNum += (int.Parse(ViewState["CurrentPageNum"].ToString()) - 1) * PageSize; } dt.Columns.Add(new DataColumn("rowN")); for (int i = 0; i < dt.Rows.Count; i++) { dt.Rows[i]["rowN"] = rowNum++; } Repeater1.DataSource = dt; Repeater1.DataBind(); } protected void PrePage_Click(object sender, EventArgs e) { int RecordCount = GetRecordCount(ViewState["ssearchCondition"].ToString()); //获取记录条数 int PageCount = RecordCount % PageSize > 0 ? RecordCount / PageSize + 1 : RecordCount / PageSize; int CurrentPageNum = (int)ViewState["CurrentPageNum"]; DataTable dt = GetListByPage(ViewState["ssearchCondition"].ToString(), PageSize * (CurrentPageNum - 2) + 1, PageSize * (CurrentPageNum - 2) + PageSize); CurrentPageNum -= 1; CurrentPage.Text = (CurrentPageNum).ToString(); TotalPageCount.Text = PageCount.ToString(); EnableJudger(); ViewState["PageCount"] = PageCount; ViewState["CurrentPageNum"] = CurrentPageNum; int rowNum = 1; if (int.Parse(ViewState["PageCount"].ToString()) > 0) { rowNum += (int.Parse(ViewState["CurrentPageNum"].ToString()) - 1) * PageSize; } dt.Columns.Add(new DataColumn("rowN")); for (int i = 0; i < dt.Rows.Count; i++) { dt.Rows[i]["rowN"] = rowNum++; } Repeater1.DataSource = dt; Repeater1.DataBind(); } protected void NextPage_Click(object sender, EventArgs e) { int RecordCount = GetRecordCount(ViewState["ssearchCondition"].ToString()); //获取记录条数 int PageCount = RecordCount % PageSize > 0 ? RecordCount / PageSize + 1 : RecordCount / PageSize; int CurrentPageNum = (int)ViewState["CurrentPageNum"]; DataTable dt = GetListByPage(ViewState["ssearchCondition"].ToString(), PageSize * (CurrentPageNum) + 1, PageSize * (CurrentPageNum) + PageSize); CurrentPageNum += 1; CurrentPage.Text = (CurrentPageNum).ToString(); TotalPageCount.Text = PageCount.ToString(); EnableJudger(); ViewState["PageCount"] = PageCount; ViewState["CurrentPageNum"] = CurrentPageNum; int rowNum = 1; if (int.Parse(ViewState["PageCount"].ToString()) > 0) { rowNum += (int.Parse(ViewState["CurrentPageNum"].ToString()) - 1) * PageSize; } dt.Columns.Add(new DataColumn("rowN")); for (int i = 0; i < dt.Rows.Count; i++) { dt.Rows[i]["rowN"] = rowNum++; } Repeater1.DataSource = dt; Repeater1.DataBind(); } protected void LastPage_Click(object sender, EventArgs e) { int RecordCount = GetRecordCount(ViewState["ssearchCondition"].ToString()); //获取记录条数 int PageCount = RecordCount % PageSize > 0 ? RecordCount / PageSize + 1 : RecordCount / PageSize; DataTable dt = GetListByPage(ViewState["ssearchCondition"].ToString(), PageSize * (PageCount - 1) + 1, PageSize * (PageCount - 1) + PageSize); int CurrentPageNum = PageCount; CurrentPage.Text = PageCount.ToString(); TotalPageCount.Text = PageCount.ToString(); EnableJudger(); ViewState["PageCount"] = PageCount; ViewState["CurrentPageNum"] = CurrentPageNum; int rowNum = 1; if (int.Parse(ViewState["PageCount"].ToString()) > 0) { rowNum += (int.Parse(ViewState["CurrentPageNum"].ToString()) - 1) * PageSize; } dt.Columns.Add(new DataColumn("rowN")); for (int i = 0; i < dt.Rows.Count; i++) { dt.Rows[i]["rowN"] = rowNum++; } Repeater1.DataSource = dt; Repeater1.DataBind(); } protected void lbtnGoPage_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtGoPage.Text)) { return; } int gotoPageNum = int.Parse(txtGoPage.Text); int RecordCount = GetRecordCount(ViewState["ssearchCondition"].ToString()); //获取记录条数 DataTable dt = new DataTable(); int CurrentPageNum = gotoPageNum; int PageCount = RecordCount % PageSize > 0 ? RecordCount / PageSize + 1 : RecordCount / PageSize; if (gotoPageNum < 1 || gotoPageNum > PageCount) { } else { dt = GetListByPage(ViewState["ssearchCondition"].ToString(), PageSize * (CurrentPageNum - 1) + 1, PageSize * (CurrentPageNum - 1) + PageSize); CurrentPage.Text = (gotoPageNum).ToString(); TotalPageCount.Text = PageCount.ToString(); EnableJudger(); ViewState["PageCount"] = PageCount; ViewState["CurrentPageNum"] = CurrentPageNum; int rowNum = 1; if (int.Parse(ViewState["PageCount"].ToString()) > 0) { rowNum += (int.Parse(ViewState["CurrentPageNum"].ToString()) - 1) * PageSize; } dt.Columns.Add(new DataColumn("rowN")); for (int i = 0; i < dt.Rows.Count; i++) { dt.Rows[i]["rowN"] = rowNum++; } Repeater1.DataSource = dt; Repeater1.DataBind(); } } private void EnableJudger() { //1. 没有数据 if (int.Parse(TotalPageCount.Text) <= 0) { FirstPage.Enabled = false; PrePage.Enabled = false; NextPage.Enabled = false; LastPage.Enabled = false; lbtnGoPage.Enabled = false; CurrentPage.Text = "0"; return; } //2. 只有一页数据 if (int.Parse(CurrentPage.Text) == 1 && int.Parse(TotalPageCount.Text) == 1) { FirstPage.Enabled = false; PrePage.Enabled = false; NextPage.Enabled = false; LastPage.Enabled = false; lbtnGoPage.Enabled = false; return; } //3. 当前为第一页,总页数大于1 if (int.Parse(CurrentPage.Text) == 1 && int.Parse(TotalPageCount.Text) > 1) { FirstPage.Enabled = false; PrePage.Enabled = false; NextPage.Enabled = true; LastPage.Enabled = true; lbtnGoPage.Enabled = true; return; } //4. 当前页为中间页 if (int.Parse(CurrentPage.Text) > 1 && int.Parse(CurrentPage.Text) < int.Parse(TotalPageCount.Text)) { FirstPage.Enabled = true; PrePage.Enabled = true; NextPage.Enabled = true; LastPage.Enabled = true; lbtnGoPage.Enabled = true; return; } //5.当前页为最后一页,总页数大于1 if (int.Parse(CurrentPage.Text) == int.Parse(TotalPageCount.Text) && int.Parse(CurrentPage.Text) > 1) { FirstPage.Enabled = true; PrePage.Enabled = true; NextPage.Enabled = false; LastPage.Enabled = false; return; } } /// <summary> /// 获取记录总数 /// </summary> public int GetRecordCount(string strWhere) { StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) from tb_ProjectRegister"); if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); } object obj = GetSingle(strSql.ToString()); if (obj == null) { return 0; } else { return Convert.ToInt32(obj); } } public static object GetSingle(string SQLString) { using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NMXT_DBConn"].ToString())) { using (SqlCommand cmd = new SqlCommand(SQLString, connection)) { try { connection.Open(); object obj = cmd.ExecuteScalar(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (System.Data.SqlClient.SqlException e) { connection.Close(); throw e; } } } } /// <summary> /// 分页获取数据列表 /// </summary> public DataTable GetListByPage(string strWhere, int startIndex, int endIndex) { StringBuilder strSql = new StringBuilder(); strSql.Append("SELECT * FROM ( "); strSql.Append(" SELECT ROW_NUMBER() OVER ("); strSql.Append("order by T.roleId asc,T.replyEnd desc"); strSql.Append(")AS Row, T.* from tb_ProjectRegister T "); if (!string.IsNullOrEmpty(strWhere.Trim())) { strSql.Append(" WHERE " + strWhere); } strSql.Append(" ) TT"); strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex); DataSet ds = Query(strSql.ToString()); DataTable newDt = ds.Tables[0].Copy(); DataColumn newCol1 = new DataColumn("Description"); DataColumn newCol2 = new DataColumn("newCol2"); DataColumn newCol3 = new DataColumn("newCol3"); DataColumn newCol4 = new DataColumn("newCol4"); DataColumn newCol5 = new DataColumn("newCol5"); DataColumn newCol6 = new DataColumn("newCol6"); DataColumn newCol7 = new DataColumn("newCol7"); DataColumn newCol8 = new DataColumn("newCol8"); newDt.Columns.Add(newCol1); newDt.Columns.Add(newCol2); newDt.Columns.Add(newCol3); newDt.Columns.Add(newCol4); newDt.Columns.Add(newCol5); newDt.Columns.Add(newCol6); newDt.Columns.Add(newCol7); newDt.Columns.Add(newCol8); Dictionary<string, string> newDic = GetRoleDescriptions(); if (newDic == null) { foreach (DataRow row in newDt.Rows) { row["Description"] = ""; if (Convert.ToDateTime(row["applyStart"]).ToString("yyyy-MM-dd") == "1900-01-01") { row["newCol2"] = ""; } else { row["newCol2"] = Convert.ToDateTime(row["applyStart"]).ToString("yyyy-MM-dd"); } if (Convert.ToDateTime(row["applyEnd"]).ToString("yyyy-MM-dd") == "1900-01-01") { row["newCol3"] = ""; } else { row["newCol3"] = Convert.ToDateTime(row["applyEnd"]).ToString("yyyy-MM-dd"); } if (Convert.ToDateTime(row["replyStart"]).ToString("yyyy-MM-dd") == "1900-01-01") { row["newCol4"] = ""; } else { row["newCol4"] = Convert.ToDateTime(row["replyStart"]).ToString("yyyy-MM-dd"); } if (Convert.ToDateTime(row["replyEnd"]).ToString("yyyy-MM-dd") == "1900-01-01") { row["newCol5"] = ""; } else { row["newCol5"] = Convert.ToDateTime(row["replyEnd"]).ToString("yyyy-MM-dd"); } if (Convert.ToDateTime(row["realEnd"]).ToString("yyyy-MM-dd") == "1900-01-01") { row["newCol6"] = ""; } else { row["newCol6"] = Convert.ToDateTime(row["realEnd"]).ToString("yyyy-MM-dd"); } if (Convert.ToDateTime(row["acceptDate_zdb"]).ToString("yyyy-MM-dd") == "1900-01-01") { row["newCol7"] = ""; } else { row["newCol7"] = Convert.ToDateTime(row["acceptDate_zdb"]).ToString("yyyy-MM-dd"); } if (Convert.ToDateTime(row["acceptDate_zjb"]).ToString("yyyy-MM-dd") == "1900-01-01") { row["newCol8"] = ""; } else { row["newCol8"] = Convert.ToDateTime(row["acceptDate_zjb"]).ToString("yyyy-MM-dd"); } } } else { foreach (DataRow row in newDt.Rows) { if (newDic.ContainsKey(row["roleId"].ToString())) { row["Description"] = newDic[row["roleId"].ToString()]; } else { row["Description"] = ""; } if (Convert.ToDateTime(row["applyStart"]).ToString("yyyy-MM-dd") == "1900-01-01") { row["newCol2"] = ""; } else { row["newCol2"] = Convert.ToDateTime(row["applyStart"]).ToString("yyyy-MM-dd"); } if (Convert.ToDateTime(row["applyEnd"]).ToString("yyyy-MM-dd") == "1900-01-01") { row["newCol3"] = ""; } else { row["newCol3"] = Convert.ToDateTime(row["applyEnd"]).ToString("yyyy-MM-dd"); } if (Convert.ToDateTime(row["replyStart"]).ToString("yyyy-MM-dd") == "1900-01-01") { row["newCol4"] = ""; } else { row["newCol4"] = Convert.ToDateTime(row["replyStart"]).ToString("yyyy-MM-dd"); } if (Convert.ToDateTime(row["replyEnd"]).ToString("yyyy-MM-dd") == "1900-01-01") { row["newCol5"] = ""; } else { row["newCol5"] = Convert.ToDateTime(row["replyEnd"]).ToString("yyyy-MM-dd"); } if (Convert.ToDateTime(row["realEnd"]).ToString("yyyy-MM-dd") == "1900-01-01") { row["newCol6"] = ""; } else { row["newCol6"] = Convert.ToDateTime(row["realEnd"]).ToString("yyyy-MM-dd"); } if (Convert.ToDateTime(row["acceptDate_zdb"]).ToString("yyyy-MM-dd") == "1900-01-01") { row["newCol7"] = ""; } else { row["newCol7"] = Convert.ToDateTime(row["acceptDate_zdb"]).ToString("yyyy-MM-dd"); } if (Convert.ToDateTime(row["acceptDate_zjb"]).ToString("yyyy-MM-dd") == "1900-01-01") { row["newCol8"] = ""; } else { row["newCol8"] = Convert.ToDateTime(row["acceptDate_zjb"]).ToString("yyyy-MM-dd"); } } } return newDt; } public static DataSet Query(string SQLString) { using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NMXT_DBConn"].ToString())) { DataSet ds = new DataSet(); try { connection.Open(); SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); command.Fill(ds, "ds"); } catch (System.Data.SqlClient.SqlException ex) { throw new Exception(ex.Message); } return ds; } } ///********************************* 数据分页结束 ******************************** #endregion
原文地址:https://www.cnblogs.com/stevenjson/p/3139736.html