datalist 分頁實現

来源:http://www.cnblogs.com/xtflz/archive/2016/04/27/5439564.html
-Advertisement-
Play Games

1.模板頁 2.數據綁定 1 public void SearchSql(int CurrentPage) 2 { 3 pds.AllowPaging = true; 4 pds.PageSize = 8; 5 pds.CurrentPageIndex = CurrentPage; 6 DataSe ...


1.模板頁

  

 1 <div style="margin-left:25px;">
 2     <asp:Label ID="order1" runat="Server" Font-Bold="True" Font-Italic="False">排序:</asp:Label>
 3     <asp:Button ID="exchange" runat="Server" Text="兌換量" BorderColor="#FFCC00" 
 4             BorderStyle="Dotted" BackColor="Silver" Font-Size="Large" 
 5             onclick="exchange_Click" />
 6     &nbsp;
 7     <asp:Button ID="Integral" runat="Server" Text="積分數" BorderColor="#FFCC00" 
 8             BorderStyle="Dotted" BackColor="Silver" Font-Size="Large" 
 9             onclick="Integral_Click" />
10     &nbsp;
11     <asp:Button ID="time" runat="Server" Text="新產品" BorderColor="#FFCC00" 
12             BorderStyle="Dotted" BackColor="Silver" Font-Size="Large" 
13             onclick="time_Click" />
14     </div>
15 
16     <br />
17 
18     <asp:DataList ID="GoodsDataList" runat="server" Width="955px" Height="80px" 
19         RepeatColumns="4" RepeatDirection=Horizontal
20         BorderStyle="Dotted" BorderColor="#FFCC00" 
21         onitemcommand="GoodsDataList_ItemCommand" 
22         onitemdatabound="GoodsDataList_ItemDataBound">
23         <FooterTemplate>
24             <table style="width:60%;">
25                 <tr>
26                     <td align="right">
27                         <asp:Label ID="labCurrentPage" runat="server" Text=""></asp:Label>
28                     </td>
29                     <td>
30                         <asp:Label ID="labPageCount" runat="server" Text=""></asp:Label>
31                     </td>
32                     <td>
33                         <asp:LinkButton ID="lnkbtnFirst" runat="server" CommandName="first">第一頁</asp:LinkButton>
34                     </td>
35                     <td>
36                         <asp:LinkButton ID="lnkbtnFront" runat="server" CommandName="pre">上一頁</asp:LinkButton>
37                     </td>
38                     <td>
39                         <asp:LinkButton ID="lnkbtnNext" runat="server" CommandName="next">下一頁</asp:LinkButton>
40                     </td>
41                     <td>
42                         <asp:LinkButton ID="lnkbtnLast" runat="server" CommandName="last">最後一頁</asp:LinkButton>
43                     </td>
44                     <td align="right">
45                         <asp:Label ID="Label1" runat="server" Text="跳轉到:" ></asp:Label>
46                     </td>
47                     <td>
48                         <asp:TextBox ID="txPage" runat="server" Width="60px"></asp:TextBox>
49                     </td>
50                     <td>
51                         <asp:Button ID="btn_OK" runat="server" Text="跳轉(GO)" Height="20px" Width="60px" CommandName="search" BorderStyle="Dotted" BackColor="#FFCC00" />
52                     </td>
53                 </tr>
54             </table>
55         </FooterTemplate>
56         <ItemTemplate>
57             <table width="100px" class="table_ellipsis">
58                 <tr>
59                     <td>
60                         <a href='<%#"GoodsDetails.aspx?GoodsID= "+Eval("GoodsId") %>'>
61                             <asp:Image ID="imgPic" runat="server" ImageUrl='<%# "../Images/"+Eval("Picture") %>'
62                                 Height="200px" Width="190px" /></a>
63                     </td>
64                 </tr>
65                 <tr>
66                     <td>
67                         <a href='<%#"GoodsDetails.aspx?GoodsID= "+Eval("GoodsId") %>'>
68                             <asp:Label ID="labGoodsName" runat="server" Text='<%#Eval("GoodsName") %>'></asp:Label>
69                         </a>
70                     </td>
71                 </tr>
72                 <tr>
73                     <td>
74                         <asp:Label ID="labUnitPrice" runat="server" Text='<%#Eval("Integral")+"分"%>' ForeColor="Red"></asp:Label>
75                     </td>
76                 </tr>
77                 <tr>
78                     <td>
79                         已被兌換:<asp:Label ID="Descript" runat="server" Text='<%#Eval("ExchangeNum") %>' ForeColor="Red" BorderStyle="NotSet"></asp:Label>
80                     </td>
81                 </tr>
82             </table>
83         </ItemTemplate>
84     </asp:DataList>
85 
86     <br />
87 
88     <hr size="1" width="1008px" color="black" "/>

2.數據綁定

 1 public void SearchSql(int CurrentPage)
 2         {
 3             pds.AllowPaging = true;
 4             pds.PageSize = 8;
 5             pds.CurrentPageIndex = CurrentPage;
 6             DataSet ds = new DataSet();
 7             conn.Open();
 8             SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
 9             sda.Fill(ds,"tb_Goods");
10             pds.DataSource = ds.Tables[0].DefaultView;
11             GoodsDataList.DataSource = pds;
12             GoodsDataList.DataBind();
13             conn.Close();
14             return ;
15         }
View Code

3.ItemCommand事件:主要響應換頁按鈕,點擊時響應   裡面的CommandName時按鈕中的屬性值

 1 protected void GoodsDataList_ItemCommand(object source, DataListCommandEventArgs e)
 2         {
 3             switch (e.CommandName)
 4             {
 5                 case "first":
 6                     pds.CurrentPageIndex = 0;
 7                     SearchSql(pds.CurrentPageIndex);
 8                     break;
 9                 case "pre":
10                     pds.CurrentPageIndex = pds.CurrentPageIndex - 1;
11                     SearchSql(pds.CurrentPageIndex);
12                     break;
13                 case "next":
14                     pds.CurrentPageIndex = pds.CurrentPageIndex + 1;
15                     SearchSql(pds.CurrentPageIndex);
16                     break;
17                 case "last":
18                     pds.CurrentPageIndex = pds.PageCount - 1;
19                     SearchSql(pds.CurrentPageIndex);
20                     break;
21                 case "search":
22                     if (e.Item.ItemType == ListItemType.Footer)
23                     {
24                         int PageCount1 = int.Parse(pds.PageCount.ToString());
25                         TextBox txPage = e.Item.FindControl("txPage") as TextBox;
26                         int MyPageNum = 0;
27                         if (txPage.Text != "")
28                         {
29                             MyPageNum = Convert.ToInt32(txPage.Text.ToString());
30                         }
31                         else
32                         {
33                             return;
34                         }
35                         if (MyPageNum <= 0 || MyPageNum > PageCount1)
36                         {
37                             Response.Write("<script>alert('請輸入頁數,並確定沒有超出總頁數!')</script>");
38                             return;
39                         }
40                         else
41                         {
42                             SearchSql(MyPageNum - 1);
43                         }
44                     }
45                     break;
46             }
47         }
View Code

4.ItemDataBound控制按鈕的可用性以及當前頁碼/總頁碼的顯示

 1   protected void GoodsDataList_ItemDataBound(object sender, DataListItemEventArgs e)
 2         {
 3             if (e.Item.ItemType == ListItemType.Footer)
 4             {
 5                 Label CurrentPage1 = e.Item.FindControl("labCurrentPage") as Label;
 6                 Label PageCount1 = e.Item.FindControl("labPageCount") as Label;
 7                 LinkButton FirstPage = e.Item.FindControl("lnkbtnFirst") as LinkButton;
 8                 LinkButton PrePage = e.Item.FindControl("lnkbtnFront") as LinkButton;
 9                 LinkButton NextPage = e.Item.FindControl("lnkbtnNext") as LinkButton;
10                 LinkButton Lastpage = e.Item.FindControl("lnkbtnLast") as LinkButton;
11                 CurrentPage1.Text = "當前第"+(pds.CurrentPageIndex + 1).ToString()+"";
12                 PageCount1.Text = "/"+""+pds.PageCount.ToString()+"";
13                 if (pds.IsFirstPage)
14                 {
15                     FirstPage.Enabled = false;
16                     PrePage.Enabled = false;
17                 }
18                 if (pds.IsLastPage)
19                 {
20                     NextPage.Enabled = false;
21                     Lastpage.Enabled = false;
22                 }
23             }
24         }
View Code

5.static PagedDataSource pds = new PagedDataSource();這個一定要註意設置成靜態變數,否則不會達到你想要的效果

效果圖:


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、Association關聯關係 說明:Assocition(關聯):描述了兩個或多個類之間的結構性關係。 (1)普通關聯: 1)Association name名稱:用以描述該關係的性質。 2)Role角色:當一個類處於關聯的某一端時,該類就在這個關係中扮演了一個特定的角色;角色是關聯中靠近它的 ...
  • 今天要實現的是給PDF文件添加圖片背景這個功能。PDF是近年來最流行的文件之一,無論是辦公還是日常生活中都經常會用到,很多時候,PDF文件的背景色都是白色,看多了難免覺得累,更換PDF的背景不僅可以讓眼睛看起來更舒服,還可以讓PDF文件看上去更美觀。如何實現?作為一名程式猿,當然要親自“操刀上陣”, ...
  • 先在資料庫中建表: 再從後臺將表取出來,然後轉化為json格式,再將其執行ToString()操作後,賦值給前臺的隱藏域。 註意引用using Newtonsoft.Json; 前臺利用js將隱藏域中的值取出,由於這個值是字元串格式,需要將它轉為json格式。 利用eval來實現。 註意引用jQue ...
  • 新框架正在逐步完善,可喜可賀的是基礎服務部分初具模樣了,給大家分享一下 由於基礎服務涉及面太廣,也沒開發完,這篇只介紹其中的類型轉化部分,命名為類型轉化基礎服務,其實就是基礎服務模塊的類型轉化子模塊 說到類型轉化必須要弄清楚.net的類型,類型都不清楚何來類型轉化 1、Primitive類型 1.1 ...
  • 用WCF技術也有一段時間了,只知道引用服務地址後調用裡面的方法就行了。之前也沒有思考wcf裡面的一些東西,現在才對WCF里的一些產生疑惑,比如,為什麼會有代理類,為什麼要有綁定,通道又是幹啥用的。 今天結合傳統的瀏覽器請求講講我個人的見解,至於WCF的基本技術點,什麼通道,ChannelFactor ...
  • 網站部署之~Windows Server | 本地部署 http://www.cnblogs.com/dunitian/p/4822808.html#iis 上次說瞭如何用ip來瀏覽網站(iis分配ip的時候可以不選ip,這樣網站的ip地址就會跟著伺服器變了【一般伺服器都是固定ip】) 添加一個角色 ...
  • 什麼是Memcache?能做什麼? 以下是百度的觀點: memcache是一套分散式的高速緩存系統,由LiveJournal的Brad Fitzpatrick開發,但目前被許多網站使用以提升網站的訪問速度,尤其對於一些大型的、需要頻繁訪問資料庫的網站訪問速度提升效果十分顯著[1] 。這是一套開放源代 ...
  • 前幾天在做資料庫實驗時,總是手動的向資料庫中添加少量的固定數據,於是就想如何向資料庫中導入大量的動態的數據?在網上瞭解了網路爬蟲,它可以幫助我們完成這項工作,關於網路爬蟲的原理和基礎知識,網上有大量的相關介紹,本人不想在累述,個人覺得下麵的文章寫得非常的好(網路爬蟲基本原理一、網路爬蟲基本原理二)。 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...