分页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中。

两款半月湾 HMBcloud 春节88折日本和美国CN2 VPS主机套餐

春节期间我们很多朋友都在忙着吃好喝好,当然有时候也会偶然的上网看看。对于我们站长用户来说,基本上需要等到初八之后才会开工,现在有空就看看是否有商家的促销。这里看到来自HMBcloud半月湾服务商有提供两款春节机房方案的VPS主机88折促销活动,分别是来自洛杉矶CN2 GIA和日本CN2的方案。八八折优惠码:CNY-GIA第一、洛杉矶CN2 GIA美国原生IP地址、72小时退款保障、三网回程CN2 ...

CloudCone闪购优惠洛杉矶MC机房VPS月$1.99 便宜可随意删除重开

CloudCone商家我们很多喜欢低价便宜VPS主机的肯定是熟悉的,个人不是特别喜欢他。因为我之前测试过几次,开通的机器IP都是不通的,需要删除且开通好几次才能得到一个可用的IP地址。当然他们家的优势也是有的,就是价格确实便宜,而且还支持删除重新开通,而且机房只有一个洛杉矶MC。实话,如果他们家能多几个机房,保持现在的特点,还是有很多市场的。CloudCone是来自美国的主机销售商,成立于2017...

BeerVM1GB内存/VDSps端口1GB,350元/月

beervm是一家国人商家,主要提供国内KVM VPS,有河南移动、广州移动等。现在预售湖南长沙联通vds,性价比高。湖南长沙vps(长沙vds),1GB内存/7GB SSD空间/10TB流量/1Gbps端口/独立IP/KVM,350元/月,有需要的可以关注一下。Beervm长沙联通vps套餐:长沙联通1G青春版(预售)长沙联通3G标准版(预售)长沙联通3G(预售)vCPU:1vCPU:2vCPU...

gridview分页为你推荐
discuznt最近搞了个论坛用的是discuz nt 的程序 网站地址是:www.wuxiankj.net 但有时经常会出现 HTTP Error 503.discuznt请问谁有Discuz!NT的QQ群?vista系统重装vista怎样重装系统?无线路由器限速设置无线路由器怎么设置限速开机滚动条开机滚动条要很长时间怎么解决?qq空间打扮如何打扮QQ空间?lockdowndiphone4s 完美越狱5.1.1时出现Could not connect to lockdownd。求救啊!!网络广告投放怎样在网络上进行广告的投放?系统分析员如何成为系统分析师?宽带接入服务器用wifi连不上服务器怎么办
广西虚拟主机 个人注册域名 万网域名证书查询 企业主机 大硬盘 pw域名 优key 青果网 美国十次啦服务器 天互数据 河南m值兑换 100m空间 亚马逊香港官网 100m独享 512mb qq金券 wordpress中文主题 稳定空间 服务器托管价格 tracker服务器 更多