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 7 <asp:Button ID="Integral" runat="Server" Text="積分數" BorderColor="#FFCC00" 8 BorderStyle="Dotted" BackColor="Silver" Font-Size="Large" 9 onclick="Integral_Click" /> 10 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();這個一定要註意設置成靜態變數,否則不會達到你想要的效果
效果圖: