GridView分页功能其实非常简单。这是一个简单的分页实现,给需要的做个参考。基于一下的思路很容易就可以做强大的分页出来
页面代码:
- <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">
- <Columns>
- <asp:BoundField DataField="ID" HeaderText="ID" />
- <asp:BoundField DataField="unionid" HeaderText="会员号" />
- <asp:BoundField DataField="username" HeaderText="用户名" />
- <asp:BoundField DataField="company" HeaderText="公司名" />
- <asp:BoundField DataField="dutyman" HeaderText="业务负责人" />
- <asp:BoundField DataField="dutymanmobile" HeaderText="业务负责人手机" />
- <asp:TemplateField HeaderText="全选">
- <ItemTemplate>
- <asp:CheckBox ID="CheckBox1" runat="server" />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="订单情况">
- <ItemTemplate>
- <asp:LinkButton ID="lb_Orders" runat="server">查看订单</asp:LinkButton>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="房态修改">
- <ItemTemplate>
- <asp:Button ID="tb_RoomStatus" runat="server" CommandName="RoomStatus" Text='<%#bind("RoomStatusFlag") %>' OnClientClick="return confirm('修改房态修改状态?');" />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="自动满房">
- <ItemTemplate>
- <asp:Button ID="tb_AutoFullRoom" runat="server" CommandName="AutoFullRoom" Text='<%#bind("AutoFullRoomFlag") %>' />
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="操作">
- <ItemTemplate>
- <asp:LinkButton ID="lb_Del" runat="server" OnClientClick="return confirm('删除此记录?')" CommandName="del">删除</asp:LinkButton>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:HyperLinkField DataNavigateUrlFields="id" DataNavigateUrlFormatString="~/Admin/Union.aspx?action=update&id={0}"
- HeaderText="确认与修改" Text="确认与修改" />
- <asp:BoundField DataField="registerdt" HeaderText="注册时间" />
- <asp:BoundField DataField="lastlogindt" HeaderText="最后登陆时间" />
- <asp:BoundField DataField="logincount" HeaderText="登陆次数" />
- </Columns>
- <PagerSettings Position="TopAndBottom" />
- <PagerTemplate>
- <table style="width: 100%">
- <tr>
- <td align="right">
- 当前页:<asp:DropDownList ID="dl_pager" runat="server" AutoPostBack="True" OnSelectedIndexChanged="dl_pager_SelectedIndexChanged">
- </asp:DropDownList>/每页:<asp:Label ID="lb_pagesize" runat="server"></asp:Label>
- 条</td>
- </tr>
- </table>
- </PagerTemplate>
- </asp:GridView>
CS代码:
- protected void gv_unionlist_RowDataBound(object sender, GridViewRowEventArgs e)
- {
- if (e.Row.RowType == DataControlRowType.Pager)
- {
- ((DropDownList)e.Row.FindControl("dl_pager")).Items.Clear();
- ((Label)e.Row.FindControl("lb_pagesize")).Text = gv_unionlist.PageSize.ToString();
- for (int i = 0; i < this.gv_unionlist.PageCount; i++)
- {
- ListItem item = new ListItem(Convert.ToString(i + 1), i.ToString());
- if (item.Value == this.gv_unionlist.PageIndex.ToString())
- item.Selected = true;
- ((DropDownList)e.Row.FindControl("dl_pager")).Items.Add(item);
- }
- }
- }
- protected void dl_pager_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (this.gv_unionlist.PageIndex != Convert.ToInt32(((DropDownList)gv_unionlist.TopPagerRow.FindControl("dl_pager")).SelectedValue))
- this.gv_unionlist.PageIndex = Convert.ToInt32(((DropDownList)gv_unionlist.TopPagerRow.FindControl("dl_pager")).SelectedValue);
- else if (this.gv_unionlist.PageIndex != Convert.ToInt32(((DropDownList)gv_unionlist.BottomPagerRow.FindControl("dl_pager")).SelectedValue))
- this.gv_unionlist.PageIndex = Convert.ToInt32(((DropDownList)gv_unionlist.BottomPagerRow.FindControl("dl_pager")).SelectedValue);
- this.searchUnion();
- }