GridView 通用分页
在项目中,遇到一个分页问题,原理很简单,但是,为了达到复用,将分页的控件和方法封装到用户控件中,这样就可以将分页的控件、代码与UI层分离。
以达到代码的复用好封装。代码清单如下:
GridViewPager.ascx前台:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="GridViewPager.ascx.cs" Inherits="Common_GridViewPager" %>
<div style="text-align:center; width:100%;">
<asp:LinkButton ID="lnkbtnFrist" runat="server" OnClick="lnkbtnFrist_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnPre" runat="server" OnClick="lnkbtnPre_Click">上一页</asp:LinkButton>
<asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
<asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnLast" runat="server" OnClick="lnkbtnLast_Click">尾页</asp:LinkButton>
跳转到第
<asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
</div>
GridViewPager.ascx后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Common_GridViewPager : System.Web.UI.UserControl
{
//属性
private GridView _MyGv;
//要分页的GridView
public GridView MyGv
{
get
{
return _MyGv;
}
set
{
_MyGv = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
BindGvData();
}
#region 分页操作部分的代码
//绑定GridView数据
private void BindGvData()
{
this.MyGv.DataBind();
BindPagerDDL();
SetPagerEnable();
this.lblCurrentPage.Text = string.Format("当前第{0}页/总共{1}页", this.MyGv.PageIndex + 1, this.MyGv.PageCount);
}
//绑定选择页下拉菜单
private void BindPagerDDL()
{
this.ddlCurrentPage.Items.Clear();
for (int i = 1; i <= this.MyGv.PageCount; i++)
{
this.ddlCurrentPage.Items.Add(i.ToString());
}
this.ddlCurrentPage.SelectedIndex = this.MyGv.PageIndex;
}
//页码下拉列表
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.MyGv.PageIndex = this.ddlCurrentPage.SelectedIndex;
BindGvData();
}
//首页
protected void lnkbtnFrist_Click(object sender, EventArgs e)
{
this.MyGv.PageIndex = 0;
BindGvData();
}
//上一页
protected void lnkbtnPre_Click(object sender, EventArgs e)
{
if (this.MyGv.PageIndex > 0)
{
this.MyGv.PageIndex = this.MyGv.PageIndex - 1;
BindGvData();
}
}
//下一页
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
if (this.MyGv.PageIndex < this.MyGv.PageCount)
{
this.MyGv.PageIndex = this.MyGv.PageIndex + 1;
BindGvData();
}
}
//尾页
protected void lnkbtnLast_Click(object sender, EventArgs e)
{
this.MyGv.PageIndex = this.MyGv.PageCount;
BindGvData();
}
/// <summary>
/// 设置“首页、上一页、下一页、尾页”可用性
/// </summary>
private void SetPagerEnable()
{
//最后一页,屏蔽“尾页,下一页”
if (ddlCurrentPage.Text == this.MyGv.PageCount.ToString())
{
lnkbtnNext.Enabled = false;
lnkbtnLast.Enabled = false;
lnkbtnFrist.Enabled = true;
lnkbtnPre.Enabled = true;
}
//第一页时,屏蔽“首页,上一页”
else if (ddlCurrentPage.Text.Equals("1"))
{
lnkbtnNext.Enabled = true;
lnkbtnLast.Enabled = true;
this.lnkbtnFrist.Enabled = false;
lnkbtnPre.Enabled = false;
}
else
{
lnkbtnFrist.Enabled = true;
lnkbtnPre.Enabled = true;
lnkbtnNext.Enabled = true;
lnkbtnLast.Enabled = true;
}
}
#endregion
}
asp.net web页面前台引入:
头部引入:
<%@ Register TagPrefix="userCtrl" TagName="Pager" Src="~/Common/GridViewPager.ascx" %>
在需要分页的GridView初引入:
<userCtrl:Pager runat="server" ID="MyPager" />
asp.net web页面后台引用:
private void Page_Load(object sender, System.EventArgs e)
{
this.MyPager.MyGv = this.GridView1;
BindGridViewData();
}
BindGridViewData();是绑定GridView数据源的方法。因为绑定数据源的方法可以是ADO.NET、存储过程、Linq to sql等等方法。
所以,为了达到分页的通用,将数据绑定与数据分页分离。