自定义分页,用户控件

//前台
<script type="text/javascript">
function PageChange() {
var path = "<%=PathUrl%>";
if (path != "" && path != null) {
location.href = path + "&currentPage=1" + "&pageSize=" + Number($(".Selectchange option:selected").val());
}
else {
location.href = "?currentPage=1" + "&pageSize=" + Number($(".Selectchange option:selected").val());
}
}

function TurnPage(type) {
var currentPage = "<%=currentPage%>" == null ? 1 : Number("<%=currentPage%>");
var maxPage = "<%=maxPage%>";
switch (type) {
case "f":
if (currentPage == 1) {
return;
}
else {
currentPage = 1;
}
break;
case "p":
if (currentPage <= 1) {
return;
}
else {
currentPage = currentPage - 1;
}
break;
case "n":
if (currentPage >= maxPage) {
return;
}
else {
currentPage = currentPage + 1;
}
break;
case "e":
if (currentPage == maxPage) return;
currentPage = maxPage;
break;
default:
break;
}
if (!isNaN(currentPage) && currentPage > 0) {
var path = "<%=PathUrl%>";
if (path != "" && path != null) {
location.href = path + "&currentPage=" + currentPage + "&pageSize=" + Number($(".Selectchange option:selected").val());
}
else {
location.href = "?currentPage=" + currentPage + "&pageSize=" + Number($(".Selectchange option:selected").val());
}
}
}
</script>
<style type="text/css">
.container {
height: 24px;
width: 98%;
}

.first {
float: left;
left: 0px;
font-size: 15px;
line-height: 24px;
}

.pagination {
width:400px;
margin: auto;
height: 24px;
}

.pagination li {
float: left;
margin-right: 5px;
border: #e2e2e2 1px solid;
}

.pagination > li > span {
position: relative;
float: left;
padding: 2px 12px;
margin-left: -1px;
color: #428bca;
text-decoration: none;
background-color: #fff;
border: 1px solid #ddd;
}

.Selectchange {
line-height: 24px;
font-size: 15px;
}

.end {
border: 0px !important;
line-height: 24px;
}
</style>
<div class="container">
<span class="first">共<%=total %>条数据</span>
<ul id="PagerID" class="pagination">
<li>
<input type="button" onclick="TurnPage('f')" id="HomePage" runat="server" name="name" value="首页"></li>
<li>
<input type="button" onclick="TurnPage('p')" id="OnPage" runat="server" name="name" value="上页" /></li>
<li>
<span>页:<asp:Label ID="lbl_CurrentPage" runat="server">0</asp:Label>/<asp:Label ID="lbl_MaxPage" runat="server">0</asp:Label></span>
</li>

<li>
<input type="button" onclick="TurnPage('n')" id="NextPage" runat="server" name="name" value="下页" /></li>
<li>
<input type="button" onclick="TurnPage('e')" id="BackPage" runat="server" name="name" value="末页" /></li>
<li class="end">
<span class="end">每页显示:<asp:DropDownList ID="Selectchange" class="Selectchange" onchange="PageChange()" runat="server">
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="20">20</asp:ListItem>
<asp:ListItem Value="30">30</asp:ListItem>
</asp:DropDownList></span></li>
</ul>
</div>

 

//后台
public partial class Pageing : System.Web.UI.UserControl
{
/// <summary>
/// 当前页码
/// </summary>
public int currentPage;
/// <summary>
/// 每页显示数量
/// </summary>
public int pageSize;
/// <summary>
/// 共计多少页
/// </summary>
public int maxPage;
/// <summary>
/// 总数据数量
/// </summary>
public int total;

/// <summary>
/// 页面路径(备注:有外加查询条件时,则赋值,没有外加查询条件时显示为空)
/// </summary>
public string PathUrl;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
#region 此处为前台分页四个按钮的控件样式(H-UI 框架按钮样式)
if (currentPage == 1)
{
this.HomePage.Attributes.Add("class", "btn size-MINI radius disabled");
this.OnPage.Attributes.Add("class", "btn size-MINI radius disabled");
}
else
{
this.HomePage.Attributes.Add("class", "btn btn-primary size-MINI radius");
this.OnPage.Attributes.Add("class", "btn btn-primary size-MINI radius");
}
if (currentPage == maxPage || maxPage == 0)
{
this.NextPage.Attributes.Add("class", "btn size-MINI radius disabled");
this.BackPage.Attributes.Add("class", "btn size-MINI radius disabled");
}
else
{
this.NextPage.Attributes.Add("class", "btn btn-primary size-MINI radius");
this.BackPage.Attributes.Add("class", "btn btn-primary size-MINI radius");
}
#endregion
}
}
/// <summary>
/// 分页方法
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="list_Model">分页数据源</param>
/// <param name="rpt_DateShow">分页绑定控件</param>
/// <param name="total">总记录数</param>
/// <param name="currentPageNext">当前页</param>
/// <param name="pageSize">每页显示数据</param>
/// <param name="Url">界面传递参数</param>
public void O_Pageing<T>(List<T> list_Model, Repeater rpt_DateShow, int _total, int _currentPage, int _pageSize, string _PathUrl)
{
currentPage = _currentPage;
PathUrl = _PathUrl;
total = _total;
pageSize = _pageSize;

//int index = Request.Url.LocalPath.LastIndexOf('/');
//string path = Request.Url.LocalPath.Substring(index);//获取当前界面路径

this.Selectchange.SelectedValue = pageSize.ToString();
maxPage = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(total) / Convert.ToDouble(pageSize)));
lbl_CurrentPage.Text = currentPage + "";
lbl_MaxPage.Text = maxPage + "";
rpt_DateShow.DataSource = list_Model;
rpt_DateShow.DataBind();
}
/// <summary>
/// 分页方法
/// </summary>
/// <param name="dt"></param>
/// <param name="rpt_DateShow"></param>
/// <param name="_total"></param>
/// <param name="_currentPage"></param>
/// <param name="pageSize"></param>
/// <param name="Url"></param>
public void O_Pageing(DataTable dt, Repeater rpt_DateShow, int _total, int _currentPage, int _pageSize, string _PathUrl)
{
currentPage = _currentPage;
PathUrl = _PathUrl;
total = _total;
pageSize = _pageSize;

//int index = Request.Url.LocalPath.LastIndexOf('/');
//string path = Request.Url.LocalPath.Substring(index);//获取当前界面路径

this.Selectchange.SelectedValue = pageSize.ToString();
maxPage = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(total) / Convert.ToDouble(pageSize)));
lbl_CurrentPage.Text = currentPage + "";
lbl_MaxPage.Text = maxPage + "";
rpt_DateShow.DataSource = dt;
rpt_DateShow.DataBind();
}
}

 

//后端,仅供参考( CRequest.GetInt为自定义封装类,用于获取传递参数)
public int currentPage = CRequest.GetInt("currentPage", 1);//当前页码
public int pageSize = CRequest.GetInt("pageSize", 10);//每页显示数量
public int total = 0;//分页总数据
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataPage();
}
}
public void DataPage()
{
string PathUrl = "ModuleList.aspx?modulename=" + name;//此处为当前页面后台传递的参数
//list_Modules 为数据源, rpt_ModelShow 为前台绑定数据控件
this.Pageing.O_Pageing<Modules>(list_Modules, rpt_ModelShow, total, currentPage, pageSize, PathUrl);
}

posted @ 2019-05-13 18:35  情殇メ传说  阅读(333)  评论(0编辑  收藏  举报