ASP.NET GridView控件实现分页跳转功能
2017-06-27 14:23
211 查看
最近刚刚接触ASP.NET开发,自己做一个小网站,用到了GridView控件的分页,但是触发不了IndexPageChanging事件,查找了很多资料,好不容易解决了,写一下我的使用心得;
注意:源码是借鉴别人的案例 点击打开链接
开发环境:
VS2010
前期准备:
1.更改GridView控件属性AllowPaging为true;
2.根据自定义需求更改控件属性PageSize;
3.更改GridView控件属性EnableViewState为true(就是这个属性难住很久);
4.设置GridView控件分页触发事件:onpageindexchanging="VersionList_PageIndexChanging";
前台代码:
在GridView控件末尾添加如下的前端代码
<PagerTemplate> 当前第: <asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label> 页/共 <asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label> 页 <asp:Button ID="ButtonFirstPage" runat="server" Text="首页" CommandArgument="First" CommandName="Page" Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>' /> <asp:Button ID="ButtonPreviousPage" runat="server" Text="上一页" CommandArgument="Prev" CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>' /> <asp:Button ID="LinkButtonNextPage" runat="server" Text="下一页" CommandArgument="Next" CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>' /> <asp:Button ID="LinkButtonLastPage" runat="server" Text="尾页" CommandArgument="Last" CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>' /> 转到第 <asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>'/> 页 <asp:Button ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-2" CommandName="Page" Text="Go"/> </PagerTemplate>
编写分页触发事件
protected void VersionList_PageIndexChanging(object sender, GridViewPageEventArgs e) { // 得到该控件 GridView theGrid = sender as GridView; int newPageIndex = 0; if (e.NewPageIndex == -3) { //点击了go按钮 TextBox txtNewPageIndex = null; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow GridViewRow pagerRow = theGrid.BottomPagerRow; if (pagerRow != null) { //得到text控件 txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox; } if (txtNewPageIndex != null) { //得到索引 newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; } } else { //点击了其他的按钮 newPageIndex = e.NewPageIndex; } //防止新索引溢出 newPageIndex = newPageIndex < 0 ? 0 : newPageIndex; newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex; //得到新的值 theGrid.PageIndex = newPageIndex; //VersionList.PageIndex = e.NewPageIndex; //重新绑定 Bind(sql_Default); }
重新生成后就可以完成分页操作了。
相关文章推荐
- asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)
- asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)-.NET教程,Asp.Net开发
- ASP.NET中Repeater控件实现分页功能
- asp.net 中gridview控件分页的实现
- asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)
- asp.net 中GridView控件实现全选及反选的功能
- asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)
- asp.net 中GridView控件实现全选及反选的功能
- asp.net 中GridView控件实现全选及反选的功能
- asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)
- asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)
- ASP.NET中的GridView控件功能基本实现
- ASP.NET用GridView控件实现购物车功能
- asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)
- ASP.NET调用Oracle分页存储过程并结合ASPnetpager分页控件 实现分页功能
- ASP.NET中Repeater控件实现分页功能
- asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)
- ASP.NET中Repeater控件实现分页功能
- Asp.net不拖GridView控件,用Table实现其分页和删除
- asp.net Datalist控件实现分页功能