熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

asp.net使用listview分頁顯示數據

2013-11-13 09:51:51  來源: .NET編程 

  學了這麼久的越來越發現net比java簡單很多雖然從程序的實現上C#和java幾乎就是相同的從寫程序的時間來看要比javaweb要快很多可以這麼說使用學習java的方法學習或者c#是不錯的選擇java就沒那麼多的控件可以給我們使用了上次我寫過一篇怎麼使用repeater控件的文章這次就接著上次的文章來做個listview控件來做個分頁顯示數據的小程序
   
    准備好一個數據集
   
         新建一個aspx頁面打開設計視圖先把objectdatasource控件拖拽進去配置一下數據源
   
    把listview拖拽進來選擇下數據源


    在listview裡手動配置下模板這裡就用表格顯示好了
   
    [html]
   
    <asp:ListView ID=ListView runat=server DataSourceID=ObjectDataSource  ItemContainerID= ItemPlaceHolder>
   
    <LayoutTemplate>
   
    <table border= bordercolor=#ff width= bordercollapse=collapse;>
   
    <thead>
   
    <tr>
   
    <td>用戶名</td>
   
    <td>密碼</td>
   
    </tr>
   
    </thead>
   
    <tbody>
   
    <asp:PlaceHolder runat=server ID=ItemPlaceHolder></asp:PlaceHolder>
   
    </tbody>
   
    </table>
   
    <asp:DataPager runat=server ID=ContactsDataPager PageSize=>
   
    <Fields>
   
    <asp:NextPreviousPagerField ShowFirstPageButton=true ShowLastPageButton=true
   
    FirstPageText=首頁 LastPageText=尾頁
   
    NextPageText=下一頁 PreviousPageText=上一頁/>
   
    </Fields>
   
    </asp:DataPager>
   
    </LayoutTemplate>
   
    <ItemTemplate>
   
    <tr>
   
    <td><%#Eval(FNAME)%></td>
   
    <td><%#Eval(FPASSWORD)%></td>
   
    </tr>
   
    </ItemTemplate>
   
    </asp:ListView>
   
    <asp:PlaceHolder runat=server ID=ItemPlaceHolder></asp:PlaceHolder>這一句實際上是用來占位的ID必須和ItemContainerID保持一致否則會出現下面的錯誤

\



  這裡有種可選的分頁分別是
   
    NextPreviousPagerFieldNumericPagerFieldTemplatePagerField
   
    幾個重要參數如下
   
    pagesize:每頁顯示的記錄數
   
    ButtonCount:顯示的分頁數例如如果有但是ButtonCount=那麼可見的頁數就是
   
    PreviousPageText:上一頁的顯示文本
   
    NextPageText:下一頁的顯示文本
   
    FirstPageText:首頁的顯示文本
   
    LastPageText:末頁的顯示文本
   
    下面是我寫的幾種分頁模板
   
    [html]
   
    <asp:NextPreviousPagerField ShowFirstPageButton=true ShowLastPageButton=true
   
    FirstPageText=首頁 LastPageText=尾頁
   
    NextPageText=下一頁 PreviousPageText=上一頁/>
   
    <asp:NumericPagerField ButtonCount= PreviousPageText=上一頁 NextPageText=下一頁 />
   
    <asp:TemplatePagerField>
   
    <PagerTemplate>
   
    <b>
   
    第
   
    <asp:Label runat=server ID=CurrentPageLabel
   
    Text=<%# ContainerTotalRowCount> ? (ContainerStartRowIndex / ContainerPageSize) + : %> />
   
    頁   共
   
    <asp:Label runat=server ID=TotalPagesLabel
   
    Text=<%# MathCeiling ((double)ContainerTotalRowCount / ContainerPageSize) %> />頁
   
    (
   
    共<asp:Label runat=server ID=TotalItemsLabel
   
    Text=<%# ContainerTotalRowCount%> />
   
    條記錄)
   
    <br />
   
    </b>
   
    </PagerTemplate>
   
    </asp:TemplatePagerField>
   
    <asp:NextPreviousPagerField
   
    ButtonType=Button
   
    ShowFirstPageButton=true
   
    ShowNextPageButton=false
   
    ShowPreviousPageButton=false />
   
    <asp:NumericPagerField
   
    PreviousPageText=< Prev
   
    NextPageText=Next >
   
    ButtonCount= />
   
    <asp:NextPreviousPagerField
   
    ButtonType=Button
   
    ShowLastPageButton=true
   
    ShowNextPageButton=false
   
    ShowPreviousPageButton=false />


   
    數據顯示效果


  下面是aspx頁面的全部代碼

  [html] <%@ Page Language=C# AutoEventWireup=true CodeBehind=indexaspxcs Inherits=repeaterindex %> 
 
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd
 
<html xmlns=
<head runat=server
    <title>無標題頁</title> 
    <style> 
     table 
      { 
         bordercollapse:collapse; 
      } 
 
    </style> 
</head> 
<body> 
    <form id=form runat=server
    <asp:ObjectDataSource ID=ObjectDataSource runat=server  
        DeleteMethod=Delete InsertMethod=Insert  
        OldValuesParameterFormatString=original_{} SelectMethod=GetData  
        TypeName=repeatersourcesM_STUDENTDataSetTableAdaptersM_STUDENTTableAdapter  
        UpdateMethod=Update
        <DeleteParameters> 
            <asp:Parameter Name=Original_FID Type=Int /> 
        </DeleteParameters> 
        <UpdateParameters> 
            <asp:Parameter Name=FNAME Type=String /> 
            <asp:Parameter Name=FPASSWORD Type=String /> 
            <asp:Parameter Name=Original_FID Type=Int /> 
        </UpdateParameters> 
        <InsertParameters> 
            <asp:Parameter Name=FNAME Type=String /> 
            <asp:Parameter Name=FPASSWORD Type=String /> 
        </InsertParameters> 
    </asp:ObjectDataSource> 
    <div> 
     
        <asp:ListView ID=ListView runat=server DataSourceID=ObjectDataSource  ItemContainerID= ItemPlaceHolder
          <LayoutTemplate> 
            <table border= bordercolor=#ff width= bordercollapse=collapse;
              <thead> 
                <tr> 
                  <td>用戶名</td> 
                  <td>密碼</td> 
                </tr> 
              </thead> 
              <tbody> 
                <asp:PlaceHolder runat=server ID=ItemPlaceHolder></asp:PlaceHolder> 
              </tbody> 
            </table> 
            <asp:DataPager runat=server ID=ContactsDataPager PageSize=
                <Fields> 
                
               
                  <asp:NextPreviousPagerField ShowFirstPageButton=true ShowLastPageButton=true 
                   FirstPageText=首頁 LastPageText=尾頁 
                   NextPageText=下一頁 PreviousPageText=上一頁/> 
 
                </Fields> 
            </asp:DataPager> 
           
          </LayoutTemplate> 
          <ItemTemplate> 
            <tr> 
              <td><%#Eval(FNAME)%></td> 
              <td><%#Eval(FPASSWORD)%></td> 
            </tr> 
          </ItemTemplate> 
           
        </asp:ListView> 
     
    </div> 
    </form> 
</body> 
</html> 
<%@ Page Language=C# AutoEventWireup=true CodeBehind=indexaspxcs Inherits=repeaterindex %>

  <!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>

  <html xmlns= >
<head runat=server>
    <title>無標題頁</title>
    <style>
     table
      {
         bordercollapse:collapse;
      }

  </style>
</head>
<body>
    <form id=form runat=server>
    <asp:ObjectDataSource ID=ObjectDataSource runat=server
        DeleteMethod=Delete InsertMethod=Insert
        OldValuesParameterFormatString=original_{} SelectMethod=GetData
        TypeName=repeatersourcesM_STUDENTDataSetTableAdaptersM_STUDENTTableAdapter
        UpdateMethod=Update>
        <DeleteParameters>
            <asp:Parameter Name=Original_FID Type=Int />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name=FNAME Type=String />
            <asp:Parameter Name=FPASSWORD Type=String />
            <asp:Parameter Name=Original_FID Type=Int />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name=FNAME Type=String />
            <asp:Parameter Name=FPASSWORD Type=String />
        </InsertParameters>
    </asp:ObjectDataSource>
    <div>
   
        <asp:ListView ID=ListView runat=server DataSourceID=ObjectDataSource  ItemContainerID= ItemPlaceHolder>
          <LayoutTemplate>
            <table border= bordercolor=#ff width= bordercollapse=collapse;>
              <thead>
                <tr>
                  <td>用戶名</td>
                  <td>密碼</td>
                </tr>
              </thead>
              <tbody>
                <asp:PlaceHolder runat=server ID=ItemPlaceHolder></asp:PlaceHolder>
              </tbody>
            </table>
            <asp:DataPager runat=server ID=ContactsDataPager PageSize=>
                <Fields>
              
             
                  <asp:NextPreviousPagerField ShowFirstPageButton=true ShowLastPageButton=true
                   FirstPageText=首頁 LastPageText=尾頁
                   NextPageText=下一頁 PreviousPageText=上一頁/>

  </Fields>
            </asp:DataPager>
         
          </LayoutTemplate>
          <ItemTemplate>
            <tr>
              <td><%#Eval(FNAME)%></td>
              <td><%#Eval(FPASSWORD)%></td>
            </tr>
          </ItemTemplate>
         
        </asp:ListView>
   
    </div>
    </form>
</body>
</html>

  代碼我上傳到資源裡大家自己下載吧有問題可以寫評論給
 


From:http://tw.wingwit.com/Article/program/net/201311/11786.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.