您的位置:首页 > 其它

GridView 后台分页

2016-05-18 10:36 218 查看
前面写了两篇 前台分页。但是数据少还行 ,条数上千,浏览器就反映迟钝,卡得要死,容易导致浏览器崩溃,用户体验非常不好 。

效果图

public partial class default: System.Web.UI.Page
{
SqlConnection connect;
SqlCommand cmd;
private int PageSize;//每页显示的条数

private int pageindex;//第几页
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//页面显示的条数
PageSize = int.Parse(ddlPageSize.SelectedItem.Value);
//当前页
pageindex = 1;
//绑定page的一些属性
Pagination pagination = PaginationFactory.Instance(PageSize, pageindex, RecordCount());
lblCurrentPage .Text = pageindex.ToString();
lblTotalPages.Text = pagination.TotalPages.ToString();

bind2(pageindex, PageSize);
}

}

/// <summary>
/// 选择显示数量
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
int pageSize = Convert.ToInt32(this.ddlPageSize.Text);

Pagination pagination = PaginationFactory.Instance(pageSize, 1,RecordCount());

this.lblTotalPages.Text = pagination.TotalPages.ToString();

bind2( pagination.CurrentPage, pagination.PageSize);
}
/// <summary>
/// 按页码 查询数据并绑定
/// </summary>
/// <param name="currentPage">当前页</param>
/// <param name="pageSize">页面显示条数</param>
public void bind2(int currentPage, int pageSize)
{
string sql = "";

sql = @"select * from (select ROW_NUMBER()
over (order by typeid) as HangHao,
*
from GoodsTypes) pageduserinfo
where HangHao between
(@currentPage -1)*@pagesize +1
and @pagesize * @currentPage";

using (connect = new SqlConnection("server=.;database=SuperMarket;uid=sa;pwd=123;"))
{
connect.Open();
cmd = new SqlCommand(sql, connect);
cmd.Parameters.AddWithValue("@currentPage", currentPage);
cmd.Parameters.AddWithValue("@pagesize", pageSize);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataKeyNames = new string[] { "typeid" };
GridView1.DataBind();
lblCurrentPage.Text = currentPage.ToString();

}
}

/// <summary>
/// 获取表的所有数据
/// </summary>
/// <returns></returns>
public int RecordCount()
{
int result = 0;
string connStr = "server=.;database = Test;uid=sa;pwd=123";
string sql = "select count(*) from userinfo";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
result = Convert.ToInt32(cmd.ExecuteScalar().ToString());
}
return result;

}
/// <summary>
/// 上一页
/// </summary>
protected void btnPrev_Click(object sender, EventArgs e)
{
int pagSize = Convert.ToInt32(this.ddlPageSize.Text);
int currentPage = Convert.ToInt32(this.lblCurrentPage.Text);

Pagination p = PaginationFactory.Instance(pagSize, currentPage, RecordCount());

bind2(p.Prev, p.PageSize);
}
//下一页
protected void btnNext_Click(object sender, EventArgs e)
{

int pagSize = Convert.ToInt32(this.ddlPageSize.Text);
int currentPage = Convert.ToInt32(this.lblCurrentPage.Text);

Pagination p = PaginationFactory.Instance(pagSize, currentPage, RecordCount());

bind2(p.Next, p.PageSize);
}
/// <summary>
/// 尾页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnLast_Click(object sender, EventArgs e)
{
int pagSize = Convert.ToInt32(this.ddlPageSize.Text);
int currentPage = Convert.ToInt32(this.lblCurrentPage.Text);//可以不需要

Pagination p = PaginationFactory.Instance(pagSize, currentPage,RecordCount());

bind2( p.Last, p.PageSize);
}
/// <summary>
/// 首页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnFirst_Click(object sender, EventArgs e)
{
int pagSize = Convert.ToInt32(this.ddlPageSize.Text);
int currentPage = 1;
Pagination p = PaginationFactory.Instance(pagSize, currentPage, RecordCount());
bind2(p.CurrentPage, p.PageSize);
}
/// <summary>
/// 跳转
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnGo_Click(object sender, EventArgs e)
{
int pageSize = Convert.ToInt32(this.ddlPageSize.Text);
int currentPage = Convert.ToInt32(this.txtToPage.Text);
Pagination pagination = PaginationFactory.Instance(pageSize, currentPage, RecordCount());
this.txtToPage.Text = "";
bind2(pagination.CurrentPage, pagination.PageSize);
}

}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: