DataPager and ListView

本文介绍了如何在ASP.NET中为ListView控件实现分页功能,并通过DataPager控件进行定制化显示。包括如何设置样式、隐藏单页情况下的分页按钮及程序化跳转到指定页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Different from GridView, ListView can’t have the paging function automatically if you set the attribute “AutoPaging=true”, and this is where the DataPager comes into play.

We can get the paging function for ListView easily with the help of the DataPager. We only need to put the control DataPager in the proper position in the <LayoutTemplate>, just like below,

   1: <asp:ListView ID="SomeListView"  runat="server" DataSourceID="SomeListViewObjectDataSource" 
   2:  onsorting="SomeListViewSortingEventHandler" OnDataBound="SomeListView_DataBound" >
   3:      
   4:     <LayoutTemplate> 
   5:     
   6:      <table id="SomeView" class="list-view">
   7:         <tr>
   8:             <th scope="col">
   9:                <asp:LinkButton ID="SortByID" runat="server" CommandName="Sort" CommandArgument="Id" Width="60px">ID</asp:LinkButton>
  10:             </th>
  11:             
  12:             <!-- other th here-->
  13:         </tr>
  14:         <tr id="itemPlaceHolder" runat="server" style="width:100%"/> 
  15:         
  16:         <tr id="DataPagerRow" runat="server">
  17:          
  18:             <td colspan="8" style="height:30px">
  19:              
  20:                 <asp:DataPager ID="SomeDataPager" runat="server" PageSize="25" PagedControlID="SomeListView" EnableViewState="false">
  21:                  
  22:                     <Fields>
  23:                         <asp:NumericPagerField ButtonType="Link" ButtonCount="10" NumericButtonCssClass="PageLinkButton" CurrentPageLabelCssClass="PageLinkButton"/>
  24:                     </Fields>
  25:                  
  26:                 </asp:DataPager> 
  27:             
  28:             </td>
  29:         </tr> 
  30:      </table>
  31:     </LayoutTemplate> 
  32:     
  33:     <ItemTemplate> 
  34:        <!--Concrete content here-->
  35:     </ItemTemplate>
  36:  
  37: </asp:ListView>

Please note that we need to set the attribute PageControlID to the ID of the ListView, SomeListView in this case. 

We can also set the style of the page link button by setting the attributes “NumericButtonCssClass” and “CurrentPageLabelCssClass”.

Till now, everything is easy and intuitive. Just one thing to note about the attribute “EnableViewState” for the DataPager. Sometimes, we may encounter some exception messages, saying failed to load ViewState information when switching between different pages. If this happens, try to set the attribute “EnableViewState” to false, this might help.

OK, another thing to point out. What if there is only one page of data for the ListView? By default, the page link button will always show no matter how many pages in total. But, from the perspective of users, showing page number is against one’s intuitive idea if there is only one page. So the question is, how can we hide the page link button when there is only one page of data in total?

The answer to this question is not difficult, as there is already a property for this purpose, “Visible”.  We can set this property to false if there is only one page. But how? Where to code this logic? OK, we need to handle an event, saying “OnDataBound” of the ListView.  This is reasonable, only when the data for the ListView is bound can we know how many pages will be shown in the page. The code is as below,

   1: protected void SomeListView_DataBound(object theSender, EventArgs theEventArgs)
   2: {
   3:     DataPager aDataPager = (DataPager)SomeListView.FindControl("SomeDataPager"); 
   4:     
   5:     if (aDataPager != null)
   6:     {
   7:         aDataPager.Visible = (aDataPager.PageSize + 1) < aDataPager.TotalRowCount;  
   8:     }
   9:      
  10: }
  11:  

This is fine. But how can we hide the table row around the page link button as well? This require some tricks. We need to find the table row in server side and then set the property “Visible” to false.  In order to get that table row, we need to give an unique ID for the table row and add the attribute “runat=server”, then the code changes to like below,

   1: protected void SomeListView_DataBound(object theSender, EventArgs theEventArgs)
   2: {
   3:     DataPager aDataPager = (DataPager)SomeListView.FindControl("SomeDataPager"); 
   4:     
   5:     if (aDataPager != null)
   6:     {
   7:         aDataPager.Visible = (aDataPager.PageSize + 1) < aDataPager.TotalRowCount; 
   8:  
   9:         HtmlTableRow aTableRow = (HtmlTableRow)SomeListView.FindControl("DataPagerRow"); 
  10:         
  11:         aTableRow.Visible = aDataPager.Visible;   
  12:     }
  13:      
  14: }
  15:  

In addition, there is another method of DataPager worthy of  mention,  that’s SetPageProperties.

This method can be used to switch to a specific page without clicking the page link button manually.  Let’s say, we can make the DataPager switch to some page when some condition is met programmically

转载于:https://www.cnblogs.com/fangwenyu/archive/2009/11/25/1610173.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值