Listview 利用Datapager进行分页
1 原文:http://lgm9128.blog.163.com/blog/static/421734292010513111851101/ 2 3 <asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" 4 OnPagePropertiesChanging="ListView1_PagePropertiesChanging"> 5 <LayoutTemplate> 6 <table> 7 <tr> 8 <td> 9 编号 10 </td> 11 12 <td> 13 姓名 14 </td> 15 </tr> 16 <div id="itemPlaceholder" runat="server"> 17 </div> 18 </table> 19 20 </LayoutTemplate> 21 <ItemTemplate> 22 <tr> 23 <td> 24 <%# Eval("ID") %> 25 </td> 26 27 <td> 28 <%# Eval("Name") %> 29 </td> 30 </tr> 31 </ItemTemplate> 32 33 </asp:ListView> 34 <div> 35 <asp:DataPager runat="server" PageSize="5" ID="DataPager1" PagedControlID="ListView1" QueryStringField="pageNumber"> 36 <Fields> 37 <asp:NextPreviousPagerField ButtonType="Button" FirstPageText="第一页" LastPageText="最后一页"NextPageText="下一页" 38 PreviousPageText="上一页" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False"></asp:NextPreviousPagerField> 39 <asp:NumericPagerField NextPageText="..." PreviousPageText="..."></asp:NumericPagerField> 40 <asp:NextPreviousPagerField ButtonType="Button" FirstPageText="第一页" 41 LastPageText="最后一页" 42 NextPageText="下一页" 43 PreviousPageText="上一页" ShowLastPageButton="True" 44 ShowNextPageButton="False" ShowPreviousPageButton="False"></asp:NextPreviousPagerField> 45 </Fields> 46 </asp:DataPager> 47 </div> 48 49 后台: 50 protected void Page_Load(object sender, EventArgs e) 51 52 { 53 54 if (!IsPostBack) 55 { 56 57 DataTestBind(); 58 } 59 } 60 61 protected void 62 Page_PreRender(object sender, EventArgs e) 63 { 64 65 //DataTestBind(); 66 } 67 68 private void DataTestBind() 69 70 { 71 DataTable dt = new DataTable(); 72 dt.Columns.Add("ID", 73 typeof(int)); 74 dt.Columns.Add("Name", typeof(string)); 75 76 77 for (int i = 0; i < 100; i++) 78 { 79 DataRow dr = 80 dt.NewRow(); 81 dr["ID"] = i + 1; 82 dr["Name"] = "lgm" 83 + i.ToString(); 84 dt.Rows.Add(dr); 85 } 86 ListView1.DataSource = dt; 87 ListView1.DataBind(); 88 } 89 90 protected void ListView1_PagePropertiesChanging(object sender, 91 PagePropertiesChangingEventArgs e) 92 { 93 94 DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false); 95 96 97 DataTestBind(); 98 }