|
// 准备并运行命令
SqlCommand cmd = PrepareCommand(countInfo);
if (cmd == null)
return;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable data = new DataTable();
adapter.Fill(data);
// 配置分页的数据源组件
if (_dataSource == null)
_dataSource = new PagedDataSource();
_dataSource.AllowCustomPaging = true;
_dataSource.AllowPaging = true;
_dataSource.CurrentPageIndex = 0;
_dataSource.PageSize = ItemsPerPage;
_dataSource.VirtualCount = countInfo.RecordCount;
_dataSource.DataSource = data.DefaultView;
}
在 NonCached 模式中,PagedDataSource 对象并不提供整个数据源,因此不能计算出要进行分页的总页数。进而必须对 AllowCustomPaging 属性进行标记,并提供数据源中的实际记录数量。通常,实际数量是使用 SELECT COUNT(*) 查询进行检索的。此模型与 DataGrid 的自定义分页几乎相同。此外,PagedDataSource 对象中选择的当前页面索引通常为 0,因为实际上已经存储了一页记录。
SqlPager 控件的实现方法就介绍到这里,下面我们介绍一下它的使用方法。
使用 SqlPager 控件
假设存在一个包含 ListBox 控件的示例页面。要使用分页程序,请确保 .aspx 页面正确地注册了该控件的程序集。 <%@ Register TagPrefix="expo" Namespace="DevCenter" Assembly="SqlPager" %>
控件的标记取决于实际设置的属性。以下标记是一个合理的示例: <asp:listbox runat="server" id="ListBox1"
Width="300px" Height="168px"
DataTextField="companyname" />
<br>
<expo:SqlPager runat="server" id="SqlPager1" Width="300px"
ControlToPaginate="ListBox1"
SelectCommand="SELECT customerid, companyname FROM customers"
ConnectionString="SERVER=localhost;DATABASE=northwind;UID=..."
SortKeyField="companyname" />
<br>
<asp:button runat="server" id="LoadFirst1" Text="加载第一页" />
除了分页程序以外,页面还包含一个列表框和一个按钮。 << 上一页 [11] [12] [13] 下一页 |