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

asp.net 分页显示数据表的数据的代码

程序员文章站 2024-03-07 21:06:45
实现代码如下: 复制代码 代码如下:using system; using system.collections; using system.configuration;...
实现代码如下:
复制代码 代码如下:

using system;
using system.collections;
using system.configuration;
using system.data;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;
using system.drawing;
namespace showdata4
{
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
gridview1.pagesize = 5; /*gridview控件在每页上显示的记录数目*/
if (gridview1.rows.count != 0) /*当记录数只显示一页时加载分页标签*/
{
control table = gridview1.controls[0];
int count = table.controls.count;
table.controls[count - 1].visible = true;
}
}
protected void gridview1_rowcreated(object sender, gridviewroweventargs e)
{
if (e.row.rowtype == datacontrolrowtype.pager) /*显示页导航控件的行*/
{
/*创建在网页上显示超链接的按钮*/
linkbutton button_indexfirst = new linkbutton();
linkbutton button_indexlast = new linkbutton();
linkbutton button_indexnext = new linkbutton();
linkbutton button_indexprevious = new linkbutton();
/*添加超链接按钮到页导航行*/
e.row.controls[0].controls.add(button_indexfirst);
e.row.controls[0].controls.add(new literalcontrol(("  "))); /*分页按钮之间用2个空格隔开*/
e.row.controls[0].controls.add(button_indexprevious);
e.row.controls[0].controls.add(new literalcontrol(("  ")));
e.row.controls[0].controls.add(button_indexnext);
e.row.controls[0].controls.add(new literalcontrol(("  ")));
e.row.controls[0].controls.add(button_indexlast);
button_indexfirst.text = "第一页";
button_indexfirst.commandname = "first";
button_indexfirst.click += new eventhandler(pagebuttonclick);
button_indexprevious.text = "上一页";
button_indexprevious.commandname = "previous";
button_indexprevious.click += new eventhandler(pagebuttonclick);
button_indexnext.text = "下一页";
button_indexnext.commandname = "next";
button_indexnext.click += new eventhandler(pagebuttonclick);
button_indexlast.text = "最后一页";
button_indexlast.commandname = "last";
button_indexlast.click += new eventhandler(pagebuttonclick);
if (gridview1.pageindex == 0)
{
if (gridview1.pagecount > 1) /*记录数所需页数大于一页*/
{
button_indexfirst.enabled = false;
button_indexprevious.enabled = false;
}
else /*记录数只需一页*/
{
button_indexfirst.enabled = false;
button_indexprevious.enabled = false;
button_indexnext.enabled = false;
button_indexlast.enabled = false;
}
}
else if (gridview1.pageindex == gridview1.pagecount - 1)
{
button_indexnext.enabled = false;
button_indexlast.enabled = false;
}
else if (gridview1.pagecount <= 0)
{
response.write("数据表中没有数据!");
button_indexfirst.enabled = false;
button_indexprevious.enabled = false;
button_indexnext.enabled = false;
button_indexlast.enabled = false;
}
}
}
protected void pagebuttonclick(object sender, eventargs e)
{
linkbutton clickedbutton = ((linkbutton)sender);
if (clickedbutton.commandname == "first") /*点击的是“第一页”按钮,页索引为0*/
{
gridview1.pageindex = 0;
}
else if (clickedbutton.commandname == "next") /*点击的是“下一页”按钮,页索引加1*/
{
if (gridview1.pageindex < gridview1.pagecount - 1)
{
gridview1.pageindex += 1;
}
}
else if (clickedbutton.commandname == "previous") /*点击的是“上一页”按钮,页索引如果大于等于1则减1*/
{
if (gridview1.pageindex >= 1)
{
gridview1.pageindex -= 1;
}
}
else if (clickedbutton.commandname == "last") /*点击的是“最后一页”按钮*/
{
gridview1.pageindex = gridview1.pagecount - 1;
}
}
}
}