ListView加DataPager在不使用LinqDataSource(DataSourceID)时如何动态分页 ,
采用后端代码方式实现listview的分页
参考
http://www.cnblogs.com/kingjiong/archive/2008/10/16/1312395.html
ASP.NET 3.5中新增的ListView、DataPager与LinqDataSource控制项可说是绝佳拍档,
不过当你不使用LinqDataSource的时候会发生一个小问题,就是当你在第一次点选DataPager
中的页码时并不会跟着跳页,而点选第二次的时候才会正常运作!所谓的「不使用LinqDataSource
的时候」是指你的资料来源的取得可能是写在Code Behind中或是使用ObjectDataSource或
SqlDataSource控制项,例如:所谓的「不使用LinqDataSource的时候」是指你的资料来源的取
<div>
<asp:ListView ID="ListView1" runat="server" OnPagePropertiesChanging="ListView1_PagePropertiesChanging">
<EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>未返回数据。</td>
</tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr style="">
<td>
<a href='PorductEdit.aspx?pid=<%# Eval("ID") %>'>编辑</a>
</td>
<td>
<asp:Label ID="WPMCLabel" runat="server" Text='<%# Eval("WPMC") %>' />
</td>
<td>
<asp:Label ID="KCSLLabel" runat="server" Text='<%# Eval("KCSL") %>' />
</td>
<td>
<asp:Label ID="DJLabel" runat="server" Text='<%# Eval("DJ") %>' />
</td>
<td>
<img src='Images/<%# Eval("TPDZ") %>' alt="无图" width="32px" height="32px" />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">ID</th>
<th runat="server">WPMC</th>
<th runat="server">KCSL</th>
<th runat="server">DJ</th>
<th runat="server">TPDZ</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr id="Tr1" runat="server">
<td id="Td1" runat="server" style="">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
</div>
C#
protected void Page_Load(object sender, EventArgs e)
{
ListView1.DataSource = ProductManager.GetAll();
ListView1.DataBind();
}
protected void ListView1_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
ListView srcListView = sender as ListView;
var dp = srcListView.FindControl("DataPager1") as DataPager;
dp.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
srcListView.DataBind();
}