zoukankan      html  css  js  c++  java
  • DataPager and ListView

    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="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

  • 相关阅读:
    RMAN 增量备份 的 对象测试
    小论工具类App的盈利之道
    linux下二进制文件比较程序
    [置顶] 对iOS开发有用的一些自动化处理脚本
    [Win8]Windows8开发笔记(八):数据绑定的基础
    NetBeans 时事通讯(刊号 # 116 Sep 11, 2010)
    域名信息证实 JavaEye 已被 CSDN 收购
    插件架构简介
    GAE for Java exception: no matching index found.
    Java 7 最快要到 2012 年中发布
  • 原文地址:https://www.cnblogs.com/fangwenyu/p/1610173.html
Copyright © 2011-2022 走看看