你的位置:首页 > ASP.net教程

[ASP.net教程]datalist 分页实现


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=Horizontal20     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             else32             {33               return;34             }35             if (MyPageNum <= 0 || MyPageNum > PageCount1)36             {37               Response.Write("<script>alert('请输入页数,并确定没有超出总页数!')</script>");38               return;39             }40             else41             {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();这个一定要注意设置成静态变量,否则不会达到你想要的效果

效果图: