Gridview自定义分页
在这里我使用Gridview中自带的分页,然后进行扩展,代码如下:
<PagerTemplate> <div class="divPager"> 第<asp:Label ID="lblCurrrentPage" runat="server" ForeColor="Red" Text="<%#(((GridView)Container.NamingContainer).PageIndex + 1) %>"></asp:Label>页 /共<asp:Label ID="lblPageCount" runat="server" CssClass="Counts" Text="<%#(((GridView)Container.NamingContainer).PageCount) %>" />页 <asp:LinkButton ID="lbnFirst" runat="Server" Text="首页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="First"></asp:LinkButton> <asp:LinkButton ID="lbnPrev" runat="server" Text="上一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' CommandName="Page" CommandArgument="Prev"></asp:LinkButton> <asp:LinkButton ID="lbnNext" runat="Server" Text="下一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Next"></asp:LinkButton> <asp:LinkButton ID="lbnLast" runat="Server" Text="尾页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>' CommandName="Page" CommandArgument="Last"></asp:LinkButton> 到第<asp:TextBox runat="server" ID="inPageNum" Width="50" MaxLength="5"></asp:TextBox>页<asp:Button ID="Button1" CommandName="go" runat="server" Text="Go" /> </div> <div class="divPageSize"> 每页显示记录数:<asp:TextBox ID="txtPageSize" runat="server" Width="25" MaxLength="3"></asp:TextBox><asp:Button ID="btnSetPageSize" runat="server" Text="设置" CommandName="SetPageSize" /> </div> <div style="float: left"> <span>总车载机数量:<asp:Label ID="lblCounts" runat="server" Text='' CssClass="Counts"></asp:Label></span> <span>在线车载机数量:<asp:Label ID="lblOnlineCounts" runat="server" Text="" CssClass="Counts"></asp:Label></span> <span>当天连上车载机数量:<asp:Label ID="lblCurrentOnlineCount" runat="server" Text="" CssClass="Counts"></asp:Label></span></div> </PagerTemplate>
后台代码如下:
protected void gvEquData_RowCommand(object sender, GridViewCommandEventArgs e) { try { switch (e.CommandName) { case "go": WebTextBox tb = (WebTextBox)gvEquData.TopPagerRow.FindControl("inPageNum"); if (CommonClass.StrIsInt(tb.Text.Trim())) { int num = Int32.Parse(tb.Text.Trim()); GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1); gvEquData_PageIndexChanging(null, ea); } else { ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "ErrorInfo", "<script>alert('输入格式有误!请重新输入');</script>", false); BindGridViewData(); } break; case "SetPageSize": WebTextBox tb2 = (WebTextBox)gvEquData.TopPagerRow.FindControl("txtPageSize"); string strPageSize = tb2.Text.Trim(); if (CommonClass.StrIsInt(strPageSize)) //&& int.Parse(strPageSize) > 0 && int.Parse(strPageSize) <= EquSearchBll.equBll.GetRows() { AppconfigClass.SetValue("EquPageSize", strPageSize); } else { ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "ErrorInfo", "<script>alert('输入格式有误!请重新输入');</script>", false); } BindGridViewData(); break; default: break; } } catch (Exception ex) { CommonClass.WriteExceptionInfo(ex.Message); } }