aspx页面用List方法查询到数据列表后,如何用DataPager控件来实现分页。
时间: 2024-05-02 16:16:51 浏览: 21
使用DataPager控件来实现分页,需要在aspx页面中添加DataPager控件,并将其与ListView控件进行绑定。
以下是一些实现步骤:
1. 在aspx页面中添加ListView控件和DataPager控件,如下所示:
```
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<!-- 数据列表模板 -->
</ItemTemplate>
</asp:ListView>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
```
2. 在ListView控件后面添加SqlDataSource控件,并设置查询语句,如下所示:
```
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM MyTable">
</asp:SqlDataSource>
```
3. 在后台代码中,需要将ListView控件的PageSize属性设置为DataPager控件的PageSize属性,如下所示:
```
protected void Page_Load(object sender, EventArgs e)
{
ListView1.PageSize = DataPager1.PageSize;
}
```
4. 最后,在ListView控件中添加PagerSettings属性,并设置为DataPager控件的PagerSettings属性,如下所示:
```
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<!-- 数据列表模板 -->
</ItemTemplate>
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" PageButtonCount="10" />
</asp:ListView>
<asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
```
这样,每次查询到的数据列表就可以通过DataPager控件进行分页显示了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)