GridView分页代码简单万能实用
程序员文章站
2024-03-04 20:48:42
复制代码 代码如下:
<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>
后台代码:
//分页
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的数据源绑定事件
复制代码 代码如下:
<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>
后台代码:
复制代码 代码如下:
//分页
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的数据源绑定事件
上一篇: Java读写Cookie记录的方法