简易的分页用户控件处理Oracle数据的分页
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);
}
}
{
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();
}
}
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
/// <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
专注于Winform开发框架/混合式开发框架、Web开发框架、Bootstrap开发框架、微信门户开发框架的研究及应用。
转载请注明出处:
撰写人:伍华聪