如何使用DataGrid分页控件?
using System;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Com.Science
{
public class PageChanedEventArgs:System.EventArgs
{
private int _newindex;
public int NewPageIndex
{
set
{
this._newindex=value;
}
get
{
return this._newindex;
}
}
public PageChanedEventArgs(int newpageindex)
{
this.NewPageIndex=newpageindex;
}
}
/// <summary>
/// 分页控件。
/// </summary>
[DefaultProperty("Author"),
ToolboxData("<{0}:PageSort runat=server></{0}:PageSort>")]
public class PageSort:System.Web.UI.WebControls.WebControl,INamingContainer
{
public PageSort()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region 属性设置
/// <summary>
/// 设置跳转到第一页的显示图象路径
/// </summary>
private string fistimageurl="";
/// <summary>
/// 设置跳转到前一页的显示图象路径
/// </summary>
private string preimageurl="";
/// <summary>
/// 设置跳转到后一页的显示图象路径
/// </summary>
private string nextimageurl="";
/// <summary>
/// 设置跳转到最后一页的显示图象路径
/// </summary>
private string lastimageurl="";
/// <summary>
/// 设置绑定的数据
/// </summary>
private DataTable _datetable;
/// <summary>
/// 跳到最前页跳转按纽
/// </summary>
private ImageButton firstbutton;
/// <summary>
/// 向前跳转按钮
/// </summary>
private ImageButton prebutton;
/// <summary>
/// 向后跳转按钮
/// </summary>
private ImageButton nextbutton;
/// <summary>
/// 跳转最后一页按钮
/// </summary>
private DropDownList ddlloop;
/// <summary>
/// 跳转到具体页数
/// </summary
private ImageButton lastbutton;
/// <summary>
/// 显示页数导航
/// </summary>
private DataGrid datagrid;
private Label currentlabe;
[Browsable(false),
Description("DataGrid的当前页"),
Category("分页"),
DefaultValue(0)]
public int CurrentPageIndex
{
set
{
if(ViewState["currentpageindex"]!=null)
ViewState["currentpageindex"]=value;
else
{
ViewState["currentpageindex"]=0;
}
}
get
{
if(ViewState["currentpageindex"]!=null)
{
return int.Parse(ViewState["currentpageindex"].ToString());
}
else
{
return 0;
}
}
}
[Browsable(true),
Description("DataGrid分页控件的作者:"),
Category("分页"),
DefaultValue("yeqing")]
public string Author
{
get{return "yeqing";
}
}
[Browsable(false),
Description("判断是不是第一次加载"),
Category("分页"),
DefaultValue(0)]
public int IsFirst
{
set
{
if(ViewState["isfirst"]!=null)
{
ViewState["isfirst"]=value;
}
else
{
ViewState["isfirst"]=1;
}
}
get
{
if(ViewState["isfirst"]!=null)
{
return int.Parse(ViewState["isfirst"].ToString());
}
else
{
return 0;
}
}
}
[Browsable(true),
Description("DataGrid的数据源"),
Category("分页"),
DefaultValue(null)]
public DataTable DataSource
{
set
{
this._datetable=value;
}
get
{
if(this._datetable==null)
throw new Exception("数据集为空");
return this._datetable;
}
}
[Browsable(false),
Description("数据集总的记录数"),
Category("分页"),
DefaultValue(null)]
public int TotalRecord
{
set
{
ViewState["record"]=value;
}
get
{
if(ViewState["record"]==null)
throw new Exception("数据集为空");
return int.Parse(ViewState["record"].ToString());
}
}
[Browsable(false),
Description("设置DataGrid"),
Category("分页"),
DefaultValue(0)]
public int PageCount
{
set{
ViewState["pagecount"]=value;
}
get
{
if(ViewState["pagecount"]!=null)
{
return int.Parse(ViewState["pagecount"].ToString());
}
else
{
return 0;
}
}
}
[Browsable(true),
Description("设置跳转到第一页的显示图象路径"),
Category("分页"),
DefaultValue("image/left_arrow2.gif")]
public string FistImagUrl
{
set
{
this.fistimageurl=value;
}
get
{
if(this.fistimageurl=="")
return "image/left_arrow2.gif";
else
return this.fistimageurl;
}
}
[Browsable(true),
Description("设置跳向前跳转的显示图象路径"),
Category("分页"),
DefaultValue("image/left_arrow.gif")]
public string PreImagUrl
{
set
{
this.preimageurl=value;
}
get
{
if(this.preimageurl=="")
return "image/left_arrow.gif";
else
return this.preimageurl;
}
}
[Browsable(true),
Description("设置跳向后跳转的显示图象路径"),
Category("分页"),
DefaultValue("image/left_arrow.gif")]
public string NextImagUrl
{
set
{
this.nextimageurl=value;
}
get
{
if(this.nextimageurl=="")
return "image/right_arrow.gif";
else
return this.nextimageurl;
}
}
[Browsable(true),
Description("设置跳跳转到最后的显示图象路径"),
Category("分页"),
DefaultValue("image/left_arrow.gif")]
public string LastImagUrl
{
set
{
this.lastimageurl=value;
}
get
{
if(this.lastimageurl=="")
return "image/right_arrow2.gif";
else
return this.lastimageurl;
}
}
[Browsable(false),
Description("设置DataGrid"),
Category("分页"),
DefaultValue(null)]
public DataGrid TargetDataGrid
{
set
{
this.datagrid=value;
}
get
{
if(this.datagrid==null)
throw new Exception("没有设置DataGrid");
return this.datagrid;
}
}
#endregion
#region 事件处理
/// <summary>
///DataGrid 数据绑定
/// </summary>
public delegate void PageIndexChangeEventHandler(object sender,PageChanedEventArgs e);
public event PageIndexChangeEventHandler PageIndexChanged;
/// <summary>
///DataGrid 数据绑定
/// </summary>
public void DataGridBind()
{
if(this.IsFirst!=0)
{
if(this.TotalRecord>this.DataSource.Rows.Count)
{
if(this.TargetDataGrid.Items.Count==1)
{
if(this.CurrentPageIndex!=0)
{
this.CurrentPageIndex=this.CurrentPageIndex-1;
this.TotalRecord=this.DataSource.Rows.Count;
PageChanedEventArgs e=new PageChanedEventArgs(this.CurrentPageIndex);
this.OnPageIndexChanged(e);
}
}
this.TotalRecord=this.DataSource.Rows.Count;
}
else if(this.TotalRecord<this.DataSource.Rows.Count)
{
this.TotalRecord=this.DataSource.Rows.Count;
}
}
this.datagrid.DataSource=this.DataSource;
this.datagrid.DataBind();
this.PageCount=this.datagrid.PageCount;
this.ddlloop_bind();
this.SetNavigation();
}
/// <summary>
///触发DataGrid绑定事件
/// </summary>
[Category("Behavior"),Description("分页控件事件")]
public void OnPageIndexChanged(PageChanedEventArgs e)
{
if(this.PageIndexChanged!=null)
this.PageIndexChanged(this.datagrid,e);
}
/// <summary>
///跳转绑定
/// </summary>
private void ddlloop_bind()
{
if(this.IsFirst!=0)
{
this.ddlloop.Items.Clear();
for(int i=1;i<=this.datagrid.PageCount;i++)
{
this.ddlloop.Items.Add(i.ToString());
}
}
}
/// <summary>
///数设置跳转页
/// </summary>
private void SetNavigation()
{
if(this.IsFirst!=0)
{
this.currentlabe.Text=(this.datagrid.CurrentPageIndex+1).ToString()+"/"+this.datagrid.PageCount.ToString();
if (this.datagrid.PageCount == 1)
{
this.firstbutton.Enabled=false;
this.prebutton.Enabled=false;
this.nextbutton.Enabled=false;
this.lastbutton.Enabled=false;
}
else if (this.datagrid.CurrentPageIndex < 1)
{
this.firstbutton.Enabled=false;
this.prebutton.Enabled=false;
this.nextbutton.Enabled=true;
this.lastbutton.Enabled=true;
}
else if (this.datagrid.CurrentPageIndex + 1 == this.datagrid.PageCount)
{
this.firstbutton.Enabled=true;
this.prebutton.Enabled=true;
this.nextbutton.Enabled=false;
this.lastbutton.Enabled=false;
}
else
{
this.firstbutton.Enabled = true;
this.prebutton.Enabled = true;
this.nextbutton.Enabled = true;
this.lastbutton.Enabled = true;
}
}
}
/// <summary>
///设置页面大小
/// </summary>
private void ddlpagesize_SelectedIndexChanged(object sender,System.EventArgs e)
{
DropDownList drop=(DropDownList)sender;
this.CurrentPageIndex=0;
this.datagrid.PageSize=int.Parse(drop.SelectedItem.Text);
PageChanedEventArgs ex=new PageChanedEventArgs(this.CurrentPageIndex);
this.OnPageIndexChanged(ex);
}
/// <summary>
///跳转到第一页
/// </summary>
private void firstbutton_Click(object sender,System.Web.UI.ImageClickEventArgs e)
{
if(this.datagrid.PageCount>0)
{
this.CurrentPageIndex =0;
PageChanedEventArgs ex=new PageChanedEventArgs(this.CurrentPageIndex);
this.OnPageIndexChanged(ex);
}
}
/// <summary>
///向前跳转
/// </summary>
private void prebutton_Click(object sender,System.Web.UI.ImageClickEventArgs e)
{
this.CurrentPageIndex =this.datagrid.CurrentPageIndex -1;
PageChanedEventArgs ex=new PageChanedEventArgs(this.CurrentPageIndex);
this.OnPageIndexChanged(ex);
}
/// <summary>
///向后跳转
/// </summary>
private void nextbutton_Click(object sender,System.Web.UI.ImageClickEventArgs e)
{
if(this.datagrid.CurrentPageIndex<this.datagrid.PageCount-1)
{
this.CurrentPageIndex =this.CurrentPageIndex +1;
PageChanedEventArgs ex=new PageChanedEventArgs(this.CurrentPageIndex);
this.OnPageIndexChanged(ex);
}
}
/// <summary>
///跳转到最后一页
/// </summary>
private void lastbutton_Click(object sender,System.Web.UI.ImageClickEventArgs e)
{
if(this.datagrid.CurrentPageIndex<this.datagrid.PageCount -1)
{
this.CurrentPageIndex=this.datagrid.PageCount-1;
PageChanedEventArgs ex=new PageChanedEventArgs(this.CurrentPageIndex);
this.OnPageIndexChanged(ex);
}
}
/// <summary>
///跳转到具体的页数
/// </summary>
private void ddlloop_SelectedIndexChanged(object sender,System.EventArgs e)
{
DropDownList drop=(DropDownList)sender;
this.CurrentPageIndex=int.Parse(drop.SelectedItem.Text)-1;
PageChanedEventArgs ex=new PageChanedEventArgs(this.CurrentPageIndex);
this.OnPageIndexChanged(ex);
}
#endregion
#region 呈现子控件
/// <summary>
/// 呈现分页控件的外观
/// </summary>
protected override void CreateChildControls()
{
HtmlTable table=new HtmlTable();
table.Border=0;
table.Width="100%";
HtmlTableRow row=new HtmlTableRow();
HtmlTableCell cell=new HtmlTableCell();
cell.Style.Add("font-size","12");
System.Web.UI.LiteralControl litel1=new LiteralControl(" ");
cell.Controls.Add(litel1);
System.Web.UI.LiteralControl litel=new LiteralControl("页面大小:");
cell.Controls.Add(litel);
DropDownList ddlpagesize=new DropDownList();
for(int i=5;i<=100;i=i+5)
{
ddlpagesize.Items.Add(i.ToString());
}
ddlpagesize.SelectedIndexChanged+=new EventHandler(ddlpagesize_SelectedIndexChanged);
ddlpagesize.AutoPostBack=true;
cell.Controls.Add(ddlpagesize);
System.Web.UI.LiteralControl litel2=new LiteralControl(" ");
cell.Controls.Add(litel2);
firstbutton=new ImageButton();
firstbutton.ImageUrl=this.FistImagUrl;
firstbutton.Click+=new ImageClickEventHandler(firstbutton_Click);
cell.Controls.Add(firstbutton);
System.Web.UI.LiteralControl litel3=new LiteralControl(" ");
cell.Controls.Add(litel3);
prebutton=new ImageButton();
prebutton.ImageUrl=this.PreImagUrl;
prebutton.Click+=new ImageClickEventHandler(prebutton_Click);
cell.Controls.Add(prebutton);
System.Web.UI.LiteralControl litel9=new LiteralControl(" ");
cell.Controls.Add(litel9);
nextbutton=new ImageButton();
nextbutton.ImageUrl=this.NextImagUrl;
nextbutton.Click+=new ImageClickEventHandler(nextbutton_Click);
cell.Controls.Add(nextbutton);
System.Web.UI.LiteralControl litel4=new LiteralControl(" ");
cell.Controls.Add(litel4);
lastbutton=new ImageButton();
lastbutton.ImageUrl=this.LastImagUrl;
lastbutton.Click+=new ImageClickEventHandler(lastbutton_Click);
cell.Controls.Add(lastbutton);
System.Web.UI.LiteralControl litel5=new LiteralControl(" ");
cell.Controls.Add(litel5);
System.Web.UI.LiteralControl litel6=new LiteralControl("当前页:");
cell.Controls.Add(litel6);
currentlabe=new Label();
cell.Controls.Add(currentlabe);
System.Web.UI.LiteralControl litel7=new LiteralControl(" ");
cell.Controls.Add(litel7);
System.Web.UI.LiteralControl litel8=new LiteralControl("跳转到:");
cell.Controls.Add(litel8);
ddlloop=new DropDownList();
ddlloop.AutoPostBack=true;
ddlloop.SelectedIndexChanged+=new EventHandler(ddlloop_SelectedIndexChanged);
if(this.PageCount>0)
{
for(int i=1;i<=this.PageCount;i++)
this.ddlloop.Items.Add(i.ToString());
}
else
{
this.ddlloop.Items.Add("0");
}
cell.Controls.Add(ddlloop);
if(this.IsFirst==0)
{
if(this.DataSource.Rows.Count>0)
this.currentlabe.Text="1"+"/"+this.datagrid.PageCount.ToString();
ddlpagesize.SelectedIndex=1;
this.CurrentPageIndex=0;
if(this.datagrid.PageCount>1)
{
this.firstbutton.Enabled=false;
this.prebutton.Enabled=false;
this.nextbutton.Enabled=true;
this.lastbutton.Enabled=true;
}
else
{
this.firstbutton.Enabled=false;
this.prebutton.Enabled=false;
this.nextbutton.Enabled=false;
this.lastbutton.Enabled=false;
}
this.TotalRecord=this.DataSource.Rows.Count;
}
this.IsFirst=1;
row.Cells.Add(cell);
table.Rows.Add(row);
Controls.Add(table);
}
#endregion
}
}
本文地址:http://www.45fan.com/dnjc/72308.html