近来一直从事Oralce数据的开发工作,处理一些报表,由于数据比较大,因此要求需要进行一定的分页处理,以便提高效率,但由于这些报表是一种轻便的开发模式,不希望引入太多的复杂的东西,而且这些报表有的是用Sql检索数据的,有些是用存储过程的。
由于基本上每个报表都会使用到分页的部分,因此这些内容最好是作为控件的方式,以便减少代码,我原来的文章中有介绍过的分页控件《
查询控件、分页控件、页面展示控件,我的Web开发三大得力助手》,这个分页控件样式很不错,不过是基于Sql进行分页的,因此我在这个基础上编写了一个用户控件来处理界面和分页内容就可以了。
这个分页控件(使用用户控件模式)减少了很多重复的代码,可以较好适应存储过程或者是Sql查询的情况,当然也具有我一贯的软件风格,好的界面。
下面我看看弄好的用户控件和页面的整合效果图先。
我们在设计时看看分页控件的界面如下所示:
这个用户控件的后台代码如下所示
public partial class PagerControl : System.Web.UI.UserControl
{
public EventHandler OnDataBind = null;
public BasePage parentPage = null;
protected void Page_Load(object sender, EventArgs e)
{
}
public void BindData(PagerInfo pageInfo)
{
parentPage.CurrentPage = pageInfo.CurrentPage;
parentPage.PageCount = pageInfo.PageCount;
parentPage.RecordCount = pageInfo.RecordCount;
this.lblCurrentPage.Text = parentPage.CurrentPage.ToString();
this.lblPageCount.Text = parentPage.PageCount.ToString();
this.lblRecordCount.Text = parentPage.RecordCount.ToString();
this.txtAbsolutePage.Text = parentPage.CurrentPage.ToString();
this.ddlPageSize.SelectedValue = parentPage.PageSize.ToString();
}
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
int pageSize = StringUtil.toInteger(this.ddlPageSize.SelectedValue);
if (pageSize > 0)
{
parentPage.PageSize = pageSize;
parentPage.CurrentPage = 1;
}
if (OnDataBind != null)
{
OnDataBind(sender, e);
}
}
protected void btnGo_Click(object sender, ImageClickEventArgs e)
{
int gotoPage = StringUtil.toInteger(this.txtAbsolutePage.Text);
parentPage.CurrentPage = gotoPage;
if (OnDataBind != null)
{
OnDataBind(sender, e);
}
}
protected void btnFirst_Click(object sender, ImageClickEventArgs e)
{
parentPage.CurrentPage = 1;
if (OnDataBind != null)
{
OnDataBind(sender, e);
}
}
protected void btnPrevious_Click(object sender, ImageClickEventArgs e)
{
parentPage.CurrentPage = parentPage.CurrentPage -1;
if (OnDataBind != null)
{
OnDataBind(sender, e);
}
}
protected void btnNext_Click(object sender, ImageClickEventArgs e)
{
parentPage.CurrentPage = parentPage.CurrentPage + 1;
if (OnDataBind != null)
{
OnDataBind(sender, e);
}
}
protected void btnLast_Click(object sender, ImageClickEventArgs e)
{
parentPage.CurrentPage = parentPage.PageCount;
if (OnDataBind != null)
{
OnDataBind(sender, e);
}
}
{
public EventHandler OnDataBind = null;
public BasePage parentPage = null;
protected void Page_Load(object sender, EventArgs e)
{
}
public void BindData(PagerInfo pageInfo)
{
parentPage.CurrentPage = pageInfo.CurrentPage;
parentPage.PageCount = pageInfo.PageCount;
parentPage.RecordCount = pageInfo.RecordCount;
this.lblCurrentPage.Text = parentPage.CurrentPage.ToString();
this.lblPageCount.Text = parentPage.PageCount.ToString();
this.lblRecordCount.Text = parentPage.RecordCount.ToString();
this.txtAbsolutePage.Text = parentPage.CurrentPage.ToString();
this.ddlPageSize.SelectedValue = parentPage.PageSize.ToString();
}
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
int pageSize = StringUtil.toInteger(this.ddlPageSize.SelectedValue);
if (pageSize > 0)
{
parentPage.PageSize = pageSize;
parentPage.CurrentPage = 1;
}
if (OnDataBind != null)
{
OnDataBind(sender, e);
}
}
protected void btnGo_Click(object sender, ImageClickEventArgs e)
{
int gotoPage = StringUtil.toInteger(this.txtAbsolutePage.Text);
parentPage.CurrentPage = gotoPage;
if (OnDataBind != null)
{
OnDataBind(sender, e);
}
}
protected void btnFirst_Click(object sender, ImageClickEventArgs e)
{
parentPage.CurrentPage = 1;
if (OnDataBind != null)
{
OnDataBind(sender, e);
}
}
protected void btnPrevious_Click(object sender, ImageClickEventArgs e)
{
parentPage.CurrentPage = parentPage.CurrentPage -1;
if (OnDataBind != null)
{
OnDataBind(sender, e);
}
}
protected void btnNext_Click(object sender, ImageClickEventArgs e)
{
parentPage.CurrentPage = parentPage.CurrentPage + 1;
if (OnDataBind != null)
{
OnDataBind(sender, e);
}
}
protected void btnLast_Click(object sender, ImageClickEventArgs e)
{
parentPage.CurrentPage = parentPage.PageCount;
if (OnDataBind != null)
{
OnDataBind(sender, e);
}
}
有了这个分页控件的界面部分,我们就不需要在每个界面中重复这些代码和控制内容了。
我们在Visual Studio中,把用户控件拖动到具体需要分页的页面中的具体位置就完成了界面的设计了,这样可以减少很多重复的代码,下面我们来看看具体的页面调用部分就知道代码是否整洁很多了。
public
partial
class
CounterStatByCar : BasePage
{
protected void Page_Load( object sender, EventArgs e)
{
this .PagerControl1.parentPage = this ;
this .PagerControl1.OnDataBind += new EventHandler(BindDataHandler);
}
private void BindDataHandler( object sender, EventArgs e)
{
BindData();
}
private DataTable BindData()
{
DataTable dt = null ;
try
{
PagerInfo pageInfo = new PagerInfo();
pageInfo.CurrentPage = this .CurrentPage; // 读取BasePage中的ViewState信息,当前页面
pageInfo.PageSize = this .PageSize; // 读取BasePage中的ViewState信息,页面大小
dt = dal.GetCounterStatByCar(beginTime, endTime, carPlate, ref pageInfo).Tables[ 0 ];
DataView dv = dt.DefaultView;
this .dg.DataSource = dv;
this .dg.DataBind();
this .PagerControl1.BindData(pageInfo); // 更新页面数据
}
catch (Exception ex)
{
LogHelper.Error(ex);
Helper.Alerts( this , " 获取数据出错 " );
}
return dt;
}
protected void btnNormalSearch_Click( object sender, ImageClickEventArgs e)
{
this .CurrentPage = 1 ;
BindData();
}
}
{
protected void Page_Load( object sender, EventArgs e)
{
this .PagerControl1.parentPage = this ;
this .PagerControl1.OnDataBind += new EventHandler(BindDataHandler);
}
private void BindDataHandler( object sender, EventArgs e)
{
BindData();
}
private DataTable BindData()
{
DataTable dt = null ;
try
{
PagerInfo pageInfo = new PagerInfo();
pageInfo.CurrentPage = this .CurrentPage; // 读取BasePage中的ViewState信息,当前页面
pageInfo.PageSize = this .PageSize; // 读取BasePage中的ViewState信息,页面大小
dt = dal.GetCounterStatByCar(beginTime, endTime, carPlate, ref pageInfo).Tables[ 0 ];
DataView dv = dt.DefaultView;
this .dg.DataSource = dv;
this .dg.DataBind();
this .PagerControl1.BindData(pageInfo); // 更新页面数据
}
catch (Exception ex)
{
LogHelper.Error(ex);
Helper.Alerts( this , " 获取数据出错 " );
}
return dt;
}
protected void btnNormalSearch_Click( object sender, ImageClickEventArgs e)
{
this .CurrentPage = 1 ;
BindData();
}
}
上面的
GetCounterStatByCar是一个获取分页数据的函数,由于在方法中传入了分页必须要的信息,当前页,页面大小,数据检索后,会修改总记录数量和页面总数量的,这些信息会显示在分页控件中。我们看看PagerInfo的类代码:
[Serializable]
public class PagerInfo
{
private int currenetPageIndex = 1 ; // 当前页码
private int pageSize = 20 ; // 每页显示的记录
private int recordCount = 0 ; // 记录总数
#region 属性变量
/// <summary>
/// 获取或设置当前页码
/// </summary>
[XmlElement(ElementName = " CurrentPageIndex " )]
public int CurrentPage
{
get { return currenetPageIndex; }
set { currenetPageIndex = value; }
}
/// <summary>
/// 获取或设置每页显示的记录
/// </summary>
[XmlElement(ElementName = " PageSize " )]
public int PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
/// <summary>
/// 获取或设置记录总数
/// </summary>
[XmlElement(ElementName = " RecordCount " )]
public int RecordCount
{
get { return recordCount; }
set { recordCount = value; }
}
/// <summary>
/// 共有多少页
/// </summary>
[XmlElement(ElementName = " PageCount " )]
public int PageCount
{
get
{
if (recordCount == 0 || pageSize == 0 )
{
return 0 ;
}
else
{
int fullPage = recordCount / pageSize;
int left = recordCount % pageSize;
if (left > 0 )
{
fullPage += 1 ;
}
return fullPage;
}
}
}
#endregion
public class PagerInfo
{
private int currenetPageIndex = 1 ; // 当前页码
private int pageSize = 20 ; // 每页显示的记录
private int recordCount = 0 ; // 记录总数
#region 属性变量
/// <summary>
/// 获取或设置当前页码
/// </summary>
[XmlElement(ElementName = " CurrentPageIndex " )]
public int CurrentPage
{
get { return currenetPageIndex; }
set { currenetPageIndex = value; }
}
/// <summary>
/// 获取或设置每页显示的记录
/// </summary>
[XmlElement(ElementName = " PageSize " )]
public int PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
/// <summary>
/// 获取或设置记录总数
/// </summary>
[XmlElement(ElementName = " RecordCount " )]
public int RecordCount
{
get { return recordCount; }
set { recordCount = value; }
}
/// <summary>
/// 共有多少页
/// </summary>
[XmlElement(ElementName = " PageCount " )]
public int PageCount
{
get
{
if (recordCount == 0 || pageSize == 0 )
{
return 0 ;
}
else
{
int fullPage = recordCount / pageSize;
int left = recordCount % pageSize;
if (left > 0 )
{
fullPage += 1 ;
}
return fullPage;
}
}
}
#endregion
在BasePage中,我们需要添加几个方法,用来做ViewSate的调用的。
#region
分页的变量
/// <summary>
/// 每页的记录数量
/// </summary>
public int PageSize
{
get
{
return GetViewState( " PageSize " , 20 );
}
set
{
SetViewState( " PageSize " , value);
}
}
/// <summary>
/// 共有几页
/// </summary>
public int PageCount
{
get
{
return GetViewState( " PageCount " , 0 );
}
set
{
SetViewState( " PageCount " , value);
}
}
/// <summary>
/// 总的记录数量
/// </summary>
public int RecordCount
{
get
{
return GetViewState( " RecordCount " , 0 );
}
set
{
SetViewState( " RecordCount " , value);
}
}
/// <summary>
/// 当前页码
/// </summary>
public int CurrentPage
{
get
{
return GetViewState( " CurrentPage " , 1 );
}
set
{
int pageIndex = (value >= PageCount) ? PageCount : value;
pageIndex = (pageIndex <= 1 ) ? 1 : pageIndex;
SetViewState( " CurrentPage " , pageIndex);
}
}
#endregion
public string GetViewState( string name, string defaultValue)
{
if (ViewState[name] != null )
{
return ViewState[name].ToString();
}
else
{
return defaultValue;
}
}
public bool GetViewState( string name, bool defaultValue)
{
if (ViewState[name] != null )
{
return ( bool )ViewState[name];
}
else
{
return defaultValue;
}
}
public int GetViewState( string name, int defaultValue)
{
if (ViewState[name] != null )
{
return ( int )ViewState[name];
}
else
{
return defaultValue;
}
}
public void SetViewState( string name, object value)
{
ViewState[name] = value;
}
#endregion
/// <summary>
/// 每页的记录数量
/// </summary>
public int PageSize
{
get
{
return GetViewState( " PageSize " , 20 );
}
set
{
SetViewState( " PageSize " , value);
}
}
/// <summary>
/// 共有几页
/// </summary>
public int PageCount
{
get
{
return GetViewState( " PageCount " , 0 );
}
set
{
SetViewState( " PageCount " , value);
}
}
/// <summary>
/// 总的记录数量
/// </summary>
public int RecordCount
{
get
{
return GetViewState( " RecordCount " , 0 );
}
set
{
SetViewState( " RecordCount " , value);
}
}
/// <summary>
/// 当前页码
/// </summary>
public int CurrentPage
{
get
{
return GetViewState( " CurrentPage " , 1 );
}
set
{
int pageIndex = (value >= PageCount) ? PageCount : value;
pageIndex = (pageIndex <= 1 ) ? 1 : pageIndex;
SetViewState( " CurrentPage " , pageIndex);
}
}
#endregion
public string GetViewState( string name, string defaultValue)
{
if (ViewState[name] != null )
{
return ViewState[name].ToString();
}
else
{
return defaultValue;
}
}
public bool GetViewState( string name, bool defaultValue)
{
if (ViewState[name] != null )
{
return ( bool )ViewState[name];
}
else
{
return defaultValue;
}
}
public int GetViewState( string name, int defaultValue)
{
if (ViewState[name] != null )
{
return ( int )ViewState[name];
}
else
{
return defaultValue;
}
}
public void SetViewState( string name, object value)
{
ViewState[name] = value;
}
#endregion
本文转自博客园伍华聪的博客,原文链接:简易的分页用户控件处理Oracle数据的分页,如需转载请自行联系原博主。