ListView 有 DataSource 使用 DataPager 翻页
ListView 无 DataSource 使用 DataPager 翻页
问题描述
点击两次才能翻页
返回上一页,内容为空
解决方法
ListView 无 DataSource 使用 DataPager 翻页
问题描述
点击两次才能翻页
返回上一页,内容为空
解决方法
ListView 有 DataSource 使用 DataPager 翻页
ListView有DataSource时,直接在 itemPlaceholder
后面放一个DataPager即可翻页。
<asp:LinqDataSource ID="LinqDataSourceSoftware" runat="server" ContextTypeName="DataClassesDataContext" EntityTypeName="" OrderBy="SoftwareName desc" TableName="Softwares"></asp:LinqDataSource> <asp:ListView ID="ListViewSoftwares" runat="server" DataSourceID="LinqDataSourceSoftware" OnItemCommand="ListViewSoftwares_ItemCommand"> <LayoutTemplate> <table cellpadding="2" runat="server" id="tblSoftLibrary" cellspacing="0" class="ListView"> <tr runat="server"> <th runat="server" style="50%;"></th> <th runat="server" style="50%;"></th> </tr> <tr runat="server" id="itemPlaceholder"/> <tr runat="server"> <td colspan="5"> <asp:DataPager runat="server" ID="DataPagerSoftwares" PagedControlID="ListViewSoftwares" PageSize="10" Class="ListViewPager" > <Fields> <asp:NextPreviousPagerField ShowFirstPageButton="true" FirstPageText="<<" ShowNextPageButton="false" ShowPreviousPageButton="false"/> <asp:NumericPagerField ButtonCount="3" /> <asp:NextPreviousPagerField ShowLastPageButton="true" LastPageText=">>" ShowNextPageButton="false" ShowPreviousPageButton="false"/> </Fields> </asp:DataPager> </td> </tr> </table> </LayoutTemplate> <ItemTemplate> <tr runat="server" > <td> <asp:Image ID="Image1" runat="server" ImageUrl='<%# ImagePathPrefix+Eval("ImagePath").ToString() %>' /> </td> <td> <asp:Label ID="LabelName" runat="server" Text='<%#Eval("SoftwareName") %>' Font-Bold="true"/> </td> </tr> </ItemTemplate> </asp:ListView>
ListView 无 DataSource 使用 DataPager 翻页
ListView无DataSource时,直接使用DataPager翻页,将会遇到以下问题
问题描述
点击两次才能翻页
翻页按钮点击一次后不会有任何反应,直到第二次点击后才翻页
返回上一页,内容为空
往前翻页,内容为空
而且翻页后的内容也是错的,第二页依然显示的是第一页开头几项
解决方法
这个问题的原因是DataPager找不到当前ListView的PageChangingProperty,所以我们得手动添加OnPagePropertiesChanging事件
OnPagePropertiesChanging
<asp:ListView ID="ListViewSoftwares" runat="server" DataSourceID="LinqDataSourceSoftware" OnItemCommand="ListViewSoftwares_ItemCommand" OnPagePropertiesChanging="ListViewSoftwares_OnPagePropertiesChanging">
protected void ListViewSoftwares_OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e) { //set current page startindex, max rows and rebind to false DataPagerSoftwares.SetPageProperties(e.StartRowIndex, e.MaximumRows, false); //rebind ListView BindListViewSoftwares(); } protected void BindListViewSoftwares() { DataClassesDataContext da = new DataClassesDataContext(); var softwares = from s in da.EMS_Softwares select s; ListViewSoftwares.DataSource = softwares; ListViewSoftwares.DataBind(); }
而且为了后台设置 DataPager 控件属性,我们得把DataPager控件挪出ListView
<asp:ListView ID="ListViewSoftwares" runat="server" OnItemCommand="ListViewSoftwares_ItemCommand" OnPagePropertiesChanging="ListViewSoftwares_OnPagePropertiesChanging"> <LayoutTemplate> <table cellpadding="2" runat="server" id="tblSoftLibrary" cellspacing="0" class="ListView"> <tr runat="server"> <th runat="server" style="50%;"></th> <th runat="server" style="50%;"></th> </tr> <tr runat="server" id="itemPlaceholder"/> </table> </LayoutTemplate> <ItemTemplate> <tr runat="server" > <td> <asp:Image ID="Image1" runat="server" ImageUrl='<%# ImagePathPrefix+Eval("ImagePath").ToString() %>' /> </td> <td> <asp:Label ID="LabelName" runat="server" Text='<%#Eval("SoftwareName") %>' Font-Bold="true"/> </td> </tr> </ItemTemplate> </asp:ListView> <tr runat="server"> <td colspan="5"> <asp:DataPager runat="server" ID="DataPagerSoftwares" PagedControlID="ListViewSoftwares" PageSize="10" Class="ListViewPager" > <Fields> <asp:NextPreviousPagerField ShowFirstPageButton="true" FirstPageText="<<" ShowNextPageButton="false" ShowPreviousPageButton="false"/> <asp:NumericPagerField ButtonCount="3" /> <asp:NextPreviousPagerField ShowLastPageButton="true" LastPageText=">>" ShowNextPageButton="false" ShowPreviousPageButton="false"/> </Fields> </asp:DataPager> </td> </tr>
参考链接:http://weblogs.asp.net/hajan/paging-listview-using-datapager-without-using-datasource-control