Posted on 2005-08-20 17:32
jjccx 阅读(1262)
评论(5) 编辑 收藏 所属分类:
C#
闲来无事,研究了下Dino Esposito大师的MsdnSqlPager(创建用于 ASP.NET 的分页程序控件),写了个自己的分页控件,运行界面如下:
使用的时候,提供ControlToPaginate(数据容器的控件的名字,可以为DataGrid,DataList,ListBox等等,只要该控件可以进行数据绑定),必需两个很重要的事件:
DataRequested:获取数据
TotalCountRequested:获取数据条目数目
还需要注意的一个是:PagingMode,该属性确定数据是缓存还是不缓存而每页都到数据库中去取。比如当你有一个分页存储过程的时候,就需要设置PagingMode为NonCached。
下面是一个演示的代码:
<TABLE id="Table1" cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
<TR>
<TD align="center"><asp:datagrid id="DataGrid1" runat="server" BorderStyle="None" BorderColor="#CC9966" BorderWidth="1px"
BackColor="White" CellPadding="4">
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
</asp:datagrid></TD>
</TR>
<TR>
<TD align="center"><cc1:pager id="Pager1" runat="server" ControlToPaginate="DataGrid1" ItemsPerPage="9" PagingMode="NonCached"
NumbericButtonCount="9"></cc1:pager></TD>
</TR>
</TABLE>
在代码文件中:
private void Page_Load(object sender, System.EventArgs e)
{
if(this.IsPostBack)
return;
this.Pager1.DataBind();//这里。。。
}
//这里。。。
private DataTable Pager1_DataRequested(object sender, JJCCX.WebControls.DataRequestedEventArgs e)
{
if(e.RequestedMode == JJCCX.WebControls.DataRequestedMode.All)
{
DataTable dt = "SELECT ID, Name, MyPassword AS Password FROM TEST1";
return dt;
}
else if(e.RequestedMode == JJCCX.WebControls.DataRequestedMode.Page)
{
DataTable dt = GetPageItems("SELECT ID, Name, MyPassword AS Password FROM TEST1 ORDER BY ID", e.PageIndex, e.ItemsPerPage);
return dt;
}
return null;
}
//这里。。。
private int Pager1_TotalCountRequested(object sender, EventArgs e)
{
string sql = "SELECT COUNT(ROWID) FROM TEST1";
object o = ExecuteScalar(CommandType.Text, sql);
return Convert.ToInt32(o);
}
表 :可以由 Pager 控件进行分页的数据绑定控件
| 控件
| 说明 |
| CheckBoxList |
从 ListControl 派生而来,显示为复选框列表。 |
| DropDownList |
从 ListControl 派生而来,显示为字符串下拉列表。 |
| ListBox |
从 ListControl 派生而来,显示为字符串可滚动列表。 |
| RadioButtonList |
从 ListControl 派生而来,显示为单选按钮列表。 |
| DataList |
从 BaseDataList 派生而来,显示为模板化数据项目列表。 |
| DataGrid |
从 BaseDataList 派生而来,显示为数据项目的表格网格。DataGrid 是唯一一个内置有功能强大的分页引擎的 ASP.NET 控件。 |
| Repeater |
|