分页GRIDVIEW两种分页代码

gridview分页  时间:2021-02-11  阅读:()

第一种分页

A.aspx文件

<%@ Page Language="C#" CodeFile="真分页.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>自定义代码实现真分页</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:GridView ID="GridView1" runat="server">

</asp:GridView>

<br />

共有记录<asp:Label ID="lblrecordamount" runat="server" BackColor="White"ForeColor="Red"></asp:Label>条&nbsp;每页显示<asp:Label

ID="lblpagesize" runat="server" BackColor="White"ForeColor="Red"></asp:Label>条<br />

共有<asp:Label ID="lblpagecount" runat="server" BackColor="White"

ForeColor="Red"></asp:Label>页&nbsp; 当前是第<asp:Label

ID="lblcurpageindex" runat="server" BackColor="White"ForeColor="Red"></asp:Label>页<br />

<br />

<asp:LinkButton ID="lbFirst" runat="server" CommandName="first"OnCommand="lbLast_Command">首页</asp:LinkButton>&nbsp;

<asp:LinkButton ID="lbPrev" runat="server" CommandName="prev"OnCommand="lbLast_Command">上一页</asp:LinkButton>

&nbsp;

<asp:LinkButton ID="lbNext" runat="server" CommandName="next"OnCommand="lbLast_Command">下一页</asp:LinkButton>&nbsp;

<asp:LinkButton ID="lbLast" runat="server" CommandName="last"OnCommand="lbLast_Command"> 尾 页 </asp:LinkButton>&nbsp; 直 接 跳 转 到<asp:DropDownList

ID="ddl" runat="server"OnSelectedIndexChanged="ddl_SelectedIndexChanged">

</asp:DropDownList>页</div>

</form>

</body>

</html>

A.aspx.cs文件using System;using System.Data;

using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page

{private int recordamount = 0; //表中的记录总数private int pagesieze = 5; //每页显示多少条记录private int pagecount = 0; //共需要分多少页private int curpageindex = 0; //当前是第几页protected void Page_Load(object sender, EventArgs e)

{

//第一次请求该页时初始化recordamount、 pagesieze、 pagecount这3个值这3个值在以后的显示中都不会改变if (!this. IsPostBack)

{

SqlDataSource ds = new SqlDataSource() ;

ds.ConnectionString

= ConfigurationManager.ConnectionStrings["gvdb"] .ConnectionString;ds.SelectCommand = "select count(*) from stuinfo";

DataView dv = (DataView)ds.Select(DataSourceSelectArguments.Empty) ;int count = int.Parse(dv[0] [0] .ToString() ) ;

//初始化表中的记录总数recordamount = count;

//初始化总页数if (recordamount % pagesieze == 0)

{pagecount = recordamount / pagesieze;

}else

{pagecount = recordamount / pagesieze + 1 ;

}

//第一次浏览该页面时显示第一页所以当前页就是第一页curpageindex = 1 ;

//用Label记下这4个变量的值这样这4个变量的值就可以在服务器和浏览器端来回传递而不需要每次显示时都计算一遍this. lblrecordamount.Text = recordamount.ToString() ;

this. lblpagesize.Text = pagesieze.ToString() ;this. lblpagecount.Text = pagecount.ToString() ;this. lblcurpageindex.Text = curpageindex.ToString() ;

//初始化DropDownList1for (int i = 1 ; i <= pagecount; i++)

{this.ddl. Items.Add(i.ToString() ) ;

}

//由于4个参数的值已经确定那么要显示的记录是从几到几就都确定了这时候直接显示就可以了

ShowInfo() ;

}

//与应用程序不同的地方是此处一定要把当前页面的索引值和分页总数的值存储起来 因为//网页跳转过程中跳转入口的不确定性后面随时要用到所以必须单独存储。

//curpageindex、 pagecount这2个值在“首页、上一页、下一页、尾页”这4个按钮的事件中需要使用所以应该在每次加载时都为这2个参数赋值curpageindex = int.Parse(this. lblcurpageindex.Text) ;pagecount = int.Parse(this. lblpagecount.Text) ;

}

//根据4个变量来显示数据的方法

public void ShowInfo()

{

SqlDataSource ds = new SqlDataSource() ;ds.ConnectionString =ConfigurationManager.ConnectionStrings["gvdb"] .ConnectionString;ds.SelectCommand = "proc_stuinfopage";ds.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;

//为存储过程的2个参数赋值int start = pagesieze * (curpageindex - 1) + 1 ;int end = pagesieze * curpageindex;ds.SelectParameters.Add("startindex", start.ToString() ) ;ds.SelectParameters.Add("endindex", end.ToString() ) ;

DataView dv = (DataView)ds.Select(DataSourceSelectArguments.Empty) ;this.GridView1.DataSource = dv;this.GridView1.AutoGenerateColumns = true;this.GridView1.DataBind() ;

//改变当前是第几页这个Label的标签this. lblcurpageindex.Text = curpageindex.ToString() ;

//实现ddl同步this.ddl.SelectedValue = curpageindex.ToString() ;//设置按钮的可用性if (curpageindex == 1)

{this. lbFirst.Enabled = false;this. lbPrev.Enabled = false;

}else

{this. lbPrev.Enabled = true;this. lbFirst.Enabled = true;

}if (curpageindex == pagecount)

{this. lbLast.Enabled = false;this. lbNext.Enabled = false;

}else

{

this. lbNext.Enabled = true;this. lbLast.Enabled = true;

}

}

// “首页、上一页、下一页、尾页”这4个按钮的事件处理程序共用一个事件处理程序protected void lbLast_Command(object sender, CommandEventArgs e)

{if (e.CommandName == "f irst") //首页就是将当前页设为1

{curpageindex = 1 ;

}else if (e.CommandName == "next") //下一页就是当前页+1

{curpageindex++;

}else if (e.CommandName == "prev") //上一页就是当前页-1

{curpageindex--;

}

else if (e.CommandName == "last") //尾页就是当前页=总页数也就是最后一页

{curpageindex = pagecount;

}

//显示数据

ShowInfo() ;

}protected void ddl_SelectedIndexChanged(object sender, EventArgs e)

{string a = this.ddl.SelectedValue;curpageindex = int.Parse(a) ;

ShowInfo() ;

}

}

代码简析看效果图就可以看出这种分页是在GridVi ew外部分页 且其绑定对象是SqlDataSource因此它的分页代码是在GridView外写代码相对叫简单但确定是效果图看上去不是那么美观。其主要实现过程是

1. 实例化4个存储变量 以分别存储表中的记录总数每页显示多少条记录共需要分多少页 当前是第几页

2. 在页面加载时 同步初始化以上4个变量并将其值用相应的label标签显示出来。如此同时初始化DropDownList1 即通过循环把分页总数依次加载到下拉列表种。然后就调用ShowInfo()方法将分页后的信息显示到GridView中。

UCloud云服务器低至年59元

最近我们是不是在讨论较多的是关于K12教育的问题,培训机构由于资本的介入确实让家长更为焦虑,对于这样的整改我们还是很支持的。实际上,在云服务器市场中,我们也看到内卷和资本的力量,各大云服务商竞争也是相当激烈,更不用说个人和小公司服务商日子确实不好过。今天有看到UCloud发布的夏季促销活动,直接提前和双十一保价挂钩。这就是说,人家直接在暑假的时候就上线双十一的活动。早年的双十一活动会提前一周到十天...

2022年腾讯云新春采购季代金券提前领 领取满减优惠券和域名优惠

2022年春节假期陆续结束,根据惯例在春节之后各大云服务商会继续开始一年的促销活动。今年二月中旬会开启新春采购季的活动,我们已经看到腾讯云商家在春节期间已经有预告活动。当时已经看到有抢先优惠促销活动,目前我们企业和个人可以领取腾讯云代金券满减活动,以及企业用户可以领取域名优惠低至.COM域名1元。 直达链接 - 腾讯云新春采购活动抢先看活动时间:2022年1月20日至2022年2月15日我们可以在...

香港九龙湾(27元) 2核2G 20元 香港沙田

弘速云是创建于2021年的品牌,运营该品牌的公司HOSU LIMITED(中文名称弘速科技有限公司)公司成立于2021年国内公司注册于2019年。HOSU LIMITED主要从事出售香港VPS、美国VPS、香港独立服务器、香港站群服务器等,目前在售VPS线路有CN2+BGP、CN2 GIA,该公司旗下产品均采用KVM虚拟化架构。可联系商家代安装iso系统。国庆活动 优惠码:hosu10-1产品介绍...

gridview分页为你推荐
手游运营手册游戏策划新手应该看那些书籍?如何免费开通黄钻怎么免费开通黄钻万网核心代理万网代理商?中国万网认证核心分销商?中国论坛大全有谁知道国内人气最高的论坛排行榜?渗透测试软件测试与渗透测试那个工作有前途显卡温度多少正常显卡温度多少正常镜像文件是什么什么叫镜像文件,作用是什么?中小企业信息化信息化为中小企业发展带来了哪些机遇开机滚动条电脑开机有滚动条的画面人人逛街人人逛街网是正品吗
国外vps租用 3322免费域名 唯品秀 免备案cdn permitrootlogin 100x100头像 200g硬盘 789电视 国外免费asp空间 东莞服务器 空间租赁 双线asp空间 工信部icp备案查询 美国迈阿密 512内存 windowsserver2008 windowsserver2008r2 cx域名 winserver2008下载 linuxvi命令 更多