您的位置:首页 > 其它

老生常谈,回顾并整理一下DataGrid分页控件【转】

2008-08-28 13:58 337 查看
·摘要

素材来自于最近完成的一个.net1.0项目。因为DataGrid自带的分页功能不够完善,所以自己写了个控件,用途是支持DataGrid的自定义分页,主要包含首页、上一页、下一页、尾页;任意跳转页;显示所有页数信息等。这个分页控件可以提供给任何需要的DataGrid使用。

·设计思路

为了让这个控件能够适用于每个页面,定义了一个接口ICurrentIndex,ICurrentIndex中定义了绑定方法BindData(),以及两个属性int CurrentIndex和int PageCount。这样,每个需要使用分页控件的页面,除了本身继承自Page外,还必须实现接口ICurrentIndex,而且实现绑定方法BindData()。当然,如果你的用户控件仅仅给一个DataGrid使用,无须这么麻烦通过接口来实现。

以下就是效果图:

<TR>

<TD><asp:datagrid id="DataGrid1" runat="server" Height="144px" Width="552px" AllowPaging="True" PageSize="5">

<PagerStyle Visible="False"></PagerStyle>

</asp:datagrid></TD>

</TR>

<TR>

<TD><uc1:navigation id="navigation" runat="server" DisplayPageNumber="false"></uc1:navigation></TD>

</TR>

2)在页面的CodeBehind(2.0中叫CodeFile)里,实现接口ICurrentIndex

public class _Default : System.Web.UI.Page, ICurrentIndex

/// <summary>

/// BindDataGrid

/// </summary>

public void BindData()

{

DataTable dt = new DataTable();

if(Session["datasource"] == null)

{

string cmdText = "SELECT top 26 ProductID,ProductName,QuantityPerUnit,UnitPrice FROM Products";

dt = SQLHelper.FillDataTable(cmdText);

Session["datasource"] = dt;

}

else

{

dt = (DataTable)Session["datasource"];

}

if(dt!= null && dt.Rows.Count > 0)

{

this.recordCount = dt.Rows.Count;

this.pageCount = (int)Math.Ceiling( this.recordCount * 1.0 / this.DataGrid1.PageSize);

ViewState["RecordCount"] = this.recordCount;

ViewState["PageCount"] = this.pageCount;

this.DataGrid1.DataSource = dt.DefaultView;

this.DataGrid1.DataBind();

}

else

{

this.DataGrid1.Visible = false;

}

this.navigation.AddPageCode(this.pageCount);

this.navigation.NavigationStateChange(this.pageCount, this.DataGrid1.PageSize, this.recordCount, this.DataGrid1.CurrentPageIndex);

}

4)实现接口中的两个属性。

public int CurrentIndex

public int PageCount

public void AddPageCode(int pageCount)

/// <summary>

/// 控制导航按钮或数字的状态

/// </summary>

public void NavigationStateChange(int pageCount, int pageSize, int recordCount, int currentIndex)

{

this.LtlPageCount.Text = pageCount.ToString();

this.LtlPageIndex.Text = "1";

this.LtlPageSize.Text = pageSize.ToString();

this.LtlRecordCount.Text = recordCount.ToString();

if( pageCount <= 1 )//( RecordCount <= PageSize )//小于等于一页

{

this.LBtnFirst.Enabled = false;

this.LBtnPrev.Enabled = false;

this.LBtnNext.Enabled = false;

this.LBtnLast.Enabled = false;

}

else //有多页

{

if( currentIndex == 0 )//当前为第一页

{

this.LBtnFirst.Enabled = false;

this.LBtnPrev.Enabled = false;

this.LBtnNext.Enabled = true;

this.LBtnNext.ForeColor = System.Drawing.Color.Red;

this.LBtnLast.Enabled = true;

this.LBtnLast.ForeColor = System.Drawing.Color.Red;

}

else if(currentIndex == pageCount - 1 )//当前为最后页

{

this.LBtnFirst.Enabled = true;

this.LBtnFirst.ForeColor = System.Drawing.Color.Red;

this.LBtnPrev.Enabled = true;

this.LBtnPrev.ForeColor = System.Drawing.Color.Red;

this.LBtnNext.Enabled = false;

this.LBtnLast.Enabled = false;

}

else //中间页

{

this.LBtnFirst.Enabled = true;

this.LBtnFirst.ForeColor = System.Drawing.Color.Red;

this.LBtnPrev.Enabled = true;

this.LBtnPrev.ForeColor = System.Drawing.Color.Red;

this.LBtnNext.Enabled = true;

this.LBtnNext.ForeColor = System.Drawing.Color.Red;

this.LBtnLast.Enabled = true;

this.LBtnLast.ForeColor = System.Drawing.Color.Red;

}

}

if(recordCount == 0)//当没有纪录时DataGrid.PageCount会显示1页

this.LtlPageCount.Text = "0";

else

this.LtlPageCount.Text = pageCount.ToString();

if(recordCount == 0)

this.LtlPageIndex.Text = "0";

else

this.LtlPageIndex.Text = (currentIndex + 1).ToString();//在有页数的情况下前台显示页数加1

this.LtlPageSize.Text = pageSize.ToString();

this.LtlRecordCount.Text = recordCount.ToString();

this.ddlPageIndex.SelectedIndex = currentIndex;

}

8)控件中跳转控制代码:主要是设置DataGrid的CurrentIndex,并调用页面中的BindData()方法。

private void btnGo_Click(object sender, System.Web.UI.ImageClickEventArgs e)

{

if(this.txtPageNumber.Text.Trim() != "")

{

int index = Convert.ToInt32(this.txtPageNumber.Text.Trim()) - 1;

if(index < 0)

{

index = 0;

}

else if(index >= Convert.ToInt32(this.LtlPageCount.Text))

{

index = Convert.ToInt32(this.LtlPageCount.Text) - 1;

}

this.txtPageNumber.Text = (index + 1).ToString();

this.iParent.CurrentIndex = index;

this.iParent.BindData();

}

else

{

this.iParent.BindData();

return;//this.RegularExpressionValidator1.IsValid = false;

}

}
完整代码下载: http://files.cnblogs.com/lxinxuan/DataGridPager.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: