服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - ASP.NET教程 - GridView分页代码简单万能实用

GridView分页代码简单万能实用

2019-10-14 13:07asp.net教程网 ASP.NET教程

GridView在使用.net技术搭建的后台,在商品列表或者是信息列表经常会出现;它的作用在于有效的管理信息,增删改查等等最主要的是还可以实现分页,这一点是无可比靡的,接下来介绍如何使用GridView实现分页,需要了解的朋友可以

代码如下:


<asp:GridView ID="GridViewHistory" runat="server" AutoGenerateColumns="False" 
CssClass="vip_table" GridLines="None" BorderStyle="None" CellPadding="0" 
ShowHeader="False" AllowPaging="true" PageSize="20" 
onpageindexchanging="GridViewHistory_PageIndexChanging"> 
<PagerTemplate> 
<asp:LinkButton ID="lb_firstpage" runat="server" onclick="lb_firstpage_Click">首页</asp:LinkButton> 
<asp:LinkButton ID="lb_previouspage" runat="server" 
onclick="lb_previouspage_Click">上一页</asp:LinkButton> 
<asp:LinkButton ID="lb_nextpage" runat="server" onclick="lb_nextpage_Click">下一页</asp:LinkButton> 
<asp:LinkButton ID="lb_lastpage" runat="server" onclick="lb_lastpage_Click">尾页</asp:LinkButton> 
第<asp:Label ID="lbl_nowpage" runat="server" Text="<%#GridViewHistory.PageIndex+1 %>" ForeColor="#db530f"></asp:Label>页/共<asp:Label 
ID="lbl_totalpage" runat="server" Text="<%#GridViewHistory.PageCount %>" ForeColor="#db530f"></asp:Label>页 
</PagerTemplate> 


GridView分页代码简单万能实用
后台代码: 

复制代码代码如下:


//分页 
protected void GridViewHistory_PageIndexChanging(object sender, GridViewPageEventArgs e) 

GridViewHistory.PageIndex = e.NewPageIndex; 
dataBinding(); 

protected void Button_search_Click(object sender, EventArgs e) 

dataBinding(); 

protected void lb_firstpage_Click(object sender, EventArgs e) 

this.GridViewHistory.PageIndex = 0; 
dataBinding(); 

protected void lb_previouspage_Click(object sender, EventArgs e) 

if (this.GridViewHistory.PageIndex > 0) 

this.GridViewHistory.PageIndex--; 
dataBinding(); 


protected void lb_nextpage_Click(object sender, EventArgs e) 

if (this.GridViewHistory.PageIndex < this.GridViewHistory.PageCount) 

this.GridViewHistory.PageIndex++; 
dataBinding(); 


protected void lb_lastpage_Click(object sender, EventArgs e) 

this.GridViewHistory.PageIndex = this.GridViewHistory.PageCount; 
dataBinding(); 


dataBinding()为GridViewHistory的数据源绑定事件

延伸 · 阅读

精彩推荐