GridView 分頁實現方法

來源:互聯網
上載者:User
GridView分頁功能其實非常簡單。這是一個簡單的分頁實現,給需要的做個參考。基於一下的思路很容易就可以做強大的分頁出來 頁面代碼:
  1. <asp:GridView ID="gv_unionlist" runat="server" AutoGenerateColumns="False" Width="100%" OnRowDataBound="gv_unionlist_RowDataBound" OnRowCommand="gv_unionlist_RowCommand" AllowPaging="True" OnRowCreated="gv_unionlist_RowCreated" PageSize="2">
  2.                     <Columns>
  3.                         <asp:BoundField DataField="ID" HeaderText="ID" />
  4.                         <asp:BoundField DataField="unionid" HeaderText="會員號" />
  5.                         <asp:BoundField DataField="username" HeaderText="使用者名稱" />
  6.                         <asp:BoundField DataField="company" HeaderText="公司名" />
  7.                         <asp:BoundField DataField="dutyman" HeaderText="業務負責人" />
  8.                         <asp:BoundField DataField="dutymanmobile" HeaderText="業務負責人手機" />
  9.                         <asp:TemplateField HeaderText="全選">
  10.                             <ItemTemplate>
  11.                                 <asp:CheckBox ID="CheckBox1" runat="server" />
  12.                             </ItemTemplate>
  13.                         </asp:TemplateField>
  14.                         <asp:TemplateField HeaderText="訂單情況">
  15.                             <ItemTemplate>
  16.                                 <asp:LinkButton ID="lb_Orders" runat="server">查看訂單</asp:LinkButton>
  17.                             </ItemTemplate>
  18.                         </asp:TemplateField>
  19.                         <asp:TemplateField HeaderText="房態修改">
  20.                             <ItemTemplate>
  21.                                  <asp:Button ID="tb_RoomStatus" runat="server" CommandName="RoomStatus" Text='<%#bind("RoomStatusFlag") %>' OnClientClick="return confirm('修改房態修改狀態?');" />
  22.                             </ItemTemplate>
  23.                         </asp:TemplateField>
  24.                         <asp:TemplateField HeaderText="自動滿房">
  25.                             <ItemTemplate>
  26.                                  <asp:Button ID="tb_AutoFullRoom" runat="server" CommandName="AutoFullRoom"  Text='<%#bind("AutoFullRoomFlag") %>' />
  27.                             </ItemTemplate>
  28.                         </asp:TemplateField>
  29.                         <asp:TemplateField HeaderText="操作">
  30.                             <ItemTemplate>
  31.                                 <asp:LinkButton ID="lb_Del" runat="server" OnClientClick="return confirm('刪除此記錄?')" CommandName="del">刪除</asp:LinkButton>
  32.                             </ItemTemplate>
  33.                         </asp:TemplateField>
  34.                         <asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="~/Admin/Union.aspx?action=update&id={0}"
  35.                             HeaderText="確認與修改" Text="確認與修改" />
  36.                         <asp:BoundField DataField="registerdt" HeaderText="註冊時間" />
  37.                         <asp:BoundField DataField="lastlogindt" HeaderText="最後登陸時間" />
  38.                         <asp:BoundField DataField="logincount" HeaderText="登陸次數" />
  39.                     </Columns>
  40.                     <PagerSettings Position="TopAndBottom" />
  41.                     <PagerTemplate>
  42.                         <table style="width: 100%">
  43.                             <tr>
  44.                                 <td align="right">
  45.                                     當前頁:<asp:DropDownList ID="dl_pager" runat="server" AutoPostBack="True" OnSelectedIndexChanged="dl_pager_SelectedIndexChanged">
  46.                                     </asp:DropDownList>/每頁:<asp:Label ID="lb_pagesize" runat="server"></asp:Label>
  47.                                     條</td>
  48.                             </tr>
  49.                         </table>
  50.                     </PagerTemplate>
  51.                 </asp:GridView>

 

CS代碼:

  1.  protected void gv_unionlist_RowDataBound(object sender, GridViewRowEventArgs e)
  2.     {
  3.         if (e.Row.RowType == DataControlRowType.Pager)
  4.         {
  5.             ((DropDownList)e.Row.FindControl("dl_pager")).Items.Clear();
  6.             ((Label)e.Row.FindControl("lb_pagesize")).Text = gv_unionlist.PageSize.ToString();
  7.             for (int i = 0; i < this.gv_unionlist.PageCount; i++)
  8.             {
  9.                 ListItem item = new ListItem(Convert.ToString(i + 1), i.ToString());
  10.                 if (item.Value == this.gv_unionlist.PageIndex.ToString())
  11.                     item.Selected = true;
  12.                 ((DropDownList)e.Row.FindControl("dl_pager")).Items.Add(item);
  13.             }
  14.         }
  15. }
  16. protected void dl_pager_SelectedIndexChanged(object sender, EventArgs e)
  17.     {
  18.         if (this.gv_unionlist.PageIndex != Convert.ToInt32(((DropDownList)gv_unionlist.TopPagerRow.FindControl("dl_pager")).SelectedValue))
  19.             this.gv_unionlist.PageIndex = Convert.ToInt32(((DropDownList)gv_unionlist.TopPagerRow.FindControl("dl_pager")).SelectedValue);
  20.         else if (this.gv_unionlist.PageIndex != Convert.ToInt32(((DropDownList)gv_unionlist.BottomPagerRow.FindControl("dl_pager")).SelectedValue))
  21.             this.gv_unionlist.PageIndex = Convert.ToInt32(((DropDownList)gv_unionlist.BottomPagerRow.FindControl("dl_pager")).SelectedValue);
  22.         this.searchUnion();
  23.     }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.