效果图:
helper:
/// <summary>
/// 高效分页
/// </summary>
/// <param name="pageIndex">当前页码</param>
/// <param name="pageSize">分页容量</param>
/// <param name="strKey">主键</param>
/// <param name="showString">显示的字段</param>
/// <param name="queryString">查询字符串,支持联合查询</param>
/// <param name="whereString">查询条件,若有条件限制则必须以where 开头</param>
/// <param name="orderString">排序规则</param>
/// <param name="pageCount">传出参数:总页数统计</param>
/// <param name="recordCount">传出参数:总记录统计</param>
/// <returns>装载记录的DataTable</returns>
public DataTable ExecutePager(int pageIndex, int pageSize, string strKey, string showString, string queryString, string whereString, string orderString, out int pageCount, out int recordCount)
{
if (pageIndex < 1) pageIndex = 1;
if (pageSize < 1) pageSize = 10;
if (string.IsNullOrEmpty(showString)) showString = "*";
if (string.IsNullOrEmpty(orderString)) orderString = strKey + " asc ";
using (SqlConnection connn = new SqlConnection(ConfigurationManager.ConnectionStrings["connStr"].ConnectionString))
{ connn.Open();
string myVw = string.Format(" ( {0} ) tempVw ", queryString);
cmd = new SqlCommand(string.Format(" select count(*) as recordCount from {0} {1}", myVw, whereString), connn);
recordCount = Convert.ToInt32(cmd.ExecuteScalar());
if ((recordCount % pageSize) > 0)
pageCount = recordCount / pageSize + 1;
else
pageCount = recordCount / pageSize;
SqlCommand cmdRecord;
if (pageIndex == 1)//第一页
{ cmdRecord = new SqlCommand(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageSize, showString, myVw, whereString, orderString), connn); }
else if (pageIndex > pageCount)//超出总页数
{ cmdRecord = new SqlCommand(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageSize, showString, myVw, "where 1=2", orderString), connn); }
else
{ int pageLowerBound = pageSize * pageIndex;
int pageUpperBound = pageLowerBound - pageSize;
string recordIDs = recordID(string.Format("select top {0} {1} from {2} {3} order by {4} ", pageLowerBound, strKey, myVw, whereString, orderString), pageUpperBound);
cmdRecord = new SqlCommand(string.Format("select {0} from {1} where {2} in ({3}) order by {4} ", showString, myVw, strKey, recordIDs, orderString), connn);
}
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmdRecord);
DataTable dt = new DataTable();
dataAdapter.Fill(dt);
connn.Close();
connn.Dispose();
return dt;
}
}
DAO:
public DataTable GetAllBoatList(int PageIndex, int PageSize, out int PageCount, out int RecordCount, string strWhere) {
string strSql = "SELECT BoatId,BoatState,RegionType,BoatManagerType,BoatType,AreaName,BoatNo,BoatName from V_Web_GetBoatList"; string strShow="BoatId,BoatState,RegionType,BoatManagerType,BoatType,AreaName,BoatNo,BoatName";
return sqlhelper.ExecutePager(PageIndex, PageSize, "BoatId", strShow, strSql, strWhere, " BoatId asc ", out PageCount, out RecordCount);
}
前台:
<div class="page_Right">
<asp:Label ID="meiye" runat="server" ></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Width="30px" Height="14px"
ontextchanged="TextBox2_TextChanged" AutoPostBack="True"></asp:TextBox>
<asp:Label ID="gong" runat="server" ></asp:Label>
<%-- <asp:Label runat="server" ID="PageMes"></asp:Label>--%>
<asp:LinkButton ID="LBHome" runat="server" CssClass="pageBtn"
onclick="LBHome_Click">[首页]</asp:LinkButton>
<asp:LinkButton ID="LBUp" runat="server" CssClass="pageBtn"
onclick="LBUp_Click">[上一页]</asp:LinkButton>
<asp:LinkButton ID="LBNext" runat="server" CssClass="pageBtn"
onclick="LBNext_Click">[下一页]</asp:LinkButton>
<asp:LinkButton ID="LBEnd" runat="server" CssClass="pageBtn"
onclick="LBEnd_Click">[尾页]</asp:LinkButton>
</div> <asp:HiddenField ID="HSelectID" runat="server" Value=""/>
<asp:HiddenField ID="HWhere" runat="server" Value=""/>
<asp:HiddenField ID="HNowPage" runat="server" Value="1"/>
<asp:HiddenField ID="HPageSize" runat="server" Value="20"/>
<asp:HiddenField ID="HAllPage" runat="server" Value="0" />
后台:
private void BindData(string strClass)
{ int DataCount = 0;
int NowPage = 1;
int AllPage = 0;
int PageSize = Convert.ToInt32(HPageSize.Value);
switch (strClass)
{ case "goto":
NowPage = Convert.ToInt32(TextBox2.Text);
break;
case "next":
NowPage = Convert.ToInt32(HNowPage.Value) + 1;
break;
case "up":
NowPage = Convert.ToInt32(HNowPage.Value) - 1;
break;
case "end":
NowPage = Convert.ToInt32(HAllPage.Value);
break;
default:
break; }
DataTable dsLog = new BoatDAO().GetAllBoatList(NowPage, PageSize, out AllPage, out DataCount, HWhere.Value);
if (dsLog.Rows.Count == 0 || AllPage == 1) { LBEnd.Enabled = false; LBHome.Enabled = false; LBNext.Enabled = false; LBUp.Enabled = false; }
else if (NowPage == 1) { LBHome.Enabled = false; LBUp.Enabled = false; LBNext.Enabled = true; LBEnd.Enabled = true; }
else if (NowPage == AllPage) { LBHome.Enabled = true; LBUp.Enabled = true; LBNext.Enabled = false; LBEnd.Enabled = false; }
else { LBEnd.Enabled = true; LBHome.Enabled = true; LBNext.Enabled = true; LBUp.Enabled = true; }
GridView1.DataSource = dsLog;
GridView1.DataBind();
//PageMes.Text = string.Format("[每页<font color=green>{0}</font>条 第<font color=red>{1}</font>页/共<font color=green>{2}</font>页 共<font color=green>{3}</font>条]", PageSize, NowPage, AllPage, DataCount); meiye.Text = string.Format("[每页<font color=green>{0}</font>条 第", PageSize);
TextBox2.Text = string.Format("{0}",NowPage);
gong.Text = string.Format("页/共<font color=green>{0}</font>页 共<font color=green>{1}</font>条]", AllPage, DataCount);
HNowPage.Value = Convert.ToString(NowPage++);
HAllPage.Value = AllPage.ToString();
}
protected void LBHome_Click(object sender, EventArgs e) { BindData(""); }
protected void LBUp_Click(object sender, EventArgs e) { BindData("up"); }
protected void LBNext_Click(object sender, EventArgs e) { BindData("next"); }
protected void LBEnd_Click(object sender, EventArgs e) { BindData("end"); }
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
BindData("goto");
}
来源:https://www.cnblogs.com/mnbvcxzsl/p/3178237.html