使用GridView控件时,我们经常遇到的一个问题就是数据的分页问题。GridView控件内置了几种分页的样式,但这些样式比较简单,而且不是很美观。那么如何自定义GridView控件的分页样式呢?这里,我们就要使用GridView控件的 PagerTemplate 了。
<PagerTemplate> <br /> <asp:Label ID="lblPage" runat="server" Text='<%# "第" + (((GridView)Container.NamingContainer).PageIndex + 1) + "页/共" + (((GridView)Container.NamingContainer).PageCount) + "页" %> '></asp:Label> <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> <br /> </PagerTemplate>
在这段代码中,第3行代码定义了当前页和总页数的信息,显示样式为“第1页/共5页”。4-7行代码分别定义了“首页”、“上一页”、“下一页”和“尾页”的样式。然后,我们看一下运行的结果吧!
这样就好看多了吧!而且简单明了!
另外,使用分页时还有注意一点:如果你的数据源是使用诸如ObjectDataSource之类的话,不需要做过多的设置,就可以工作的很好。但如果你的数据源是在程序中动态创建的话,则还要在pageIndexChanging和PageIndexChanged事件处理程序中稍作处理,否则会抛出异常!
举个例子:
protected void Page_Load(object sender, EventArgs e) { if (!string.IsNullOrEmpty(this.Request.QueryString["key"])) { KeyStr = Server.UrlDecode(this.Request.QueryString["key"].ToString()); } if(!IsPostBack) { BindResource(); } } private void BindResource() { resources = Resource.GetResourcesByTitle(KeyStr); if (resources != null) { this.gvwResources.DataSource = resources; this.gvwResources.DataBind(); } this.lbltext.Text = KeyStr; }
这个例子中,gvwResources是一个GridView类的实例,我们在Page_Load事件处理程序中为其创建了一个List<Resources>类型的数据源resources。此时,我们就需要对gvwResources对象的PageIndexChanging和PageIndexChanged事件进行处理。代码如下
protected void gvwResources_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvwResources.PageIndex = e.NewPageIndex; } protected void gvwResources_PageIndexChanged(object sender, EventArgs e) { BindResource(); }
在PageIndexChanging事件处理程序中,将要显示的新页的值赋给gvwResources的PageIndex属性,在PageIndexChanged事件处理程序中,则将数据源重新绑定到gvwResources对象上。这样,自定义的分页按钮就能很好的工作了!
第二个例子:
<asp:GridView ID="grdPagerB" AllowPaging="true" runat="server" PageSize="10" AutoGenerateColumns=true OnPageIndexChanged="grdPagerB_PageIndexChanged" OnPageIndexChanging="grdPagerB_PageIndexChanging"> <PagerStyle /> <PagerTemplate> <asp:Label ID="Lab_PageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label> <asp:Label ID="Lab_CurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label> <asp:LinkButton ID="LBtn_FirstPage" runat="server" CommandArgument="First" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton> <asp:LinkButton ID="LBtn_PreviousPage" runat="server" CommandArgument="Prev" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton> <asp:LinkButton ID="LBtn_NextPage" runat="server" CommandArgument="Next" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton> <asp:LinkButton ID="LBtn_LastPage" runat="server" CommandArgument="Last" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</asp:LinkButton> </PagerTemplate> </asp:GridView>
后台代码:
protected void grdPagerB_PageIndexChanged(object sender, EventArgs e) { //进行分页之后,重新部署数据 BindDataB(); } protected void grdPagerB_PageIndexChanging(object sender, GridViewPageEventArgs e) { //分页完成之前 grdPagerB.PageIndex = e.NewPageIndex; } private void BindDataB() { string sql = "SELECT [AccessMode],[useGroupName],[Remarks],[AccessIndex],[ChangeOn],[ChangeBy],[Active] FROM [LicMaintenance].[dbo].[acsAccessMode]"; NewDbHelperSQL instance = new NewDbHelperSQL(connectionString); try { DataSet ds = instance.Query(sql, null); grdPagerB.DataSource = ds.Tables[0]; grdPagerB.DataBind(); ////用lblCurrentIndex来显示当前页的页数。 //LabelCurrentPage.Text = "第 " + (grdPagerB.PageIndex + 1).ToString() + " 页"; ////用LblPageCount来显示当前数据的总页数。 //LabelPageCount.Text = "共 " + grdPagerB.PageCount.ToString() + " 页"; ////用LblrecordCount来显示数据的总条数。 //LabelRecordCount.Text = "总共 " + ds.Tables[0].Rows.Count.ToString() + " 条"; //// 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页" ////点击首页设定的值为1。 //LinkButtonFirstPage.CommandName = "1"; ////点击‘上一页’ //LinkButtonPreviousPage.CommandName = (grdPagerB.PageIndex == 0 ? "1" : grdPagerB.PageIndex.ToString()); ////点击‘下一页’ //LinkButtonNextPage.CommandName = (grdPagerB.PageCount == 1 ? grdPagerB.PageCount.ToString() : (grdPagerB.PageIndex + 2).ToString()); ////点击‘尾页’ //LinkButtonLastPage.CommandName = grdPagerB.PageCount.ToString(); } catch (Exception ex) { } finally { if (instance != null) { instance.CloseConnection(); } } }