欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

GridView分页代码简单万能实用

程序员文章站 2024-03-06 08:14:37
复制代码 代码如下:
复制代码 代码如下:

<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的数据源绑定事件