第一种分页
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>条 每页显示<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>页 当前是第<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>
<asp:LinkButton ID="lbPrev" runat="server" CommandName="prev"OnCommand="lbLast_Command">上一页</asp:LinkButton>
<asp:LinkButton ID="lbNext" runat="server" CommandName="next"OnCommand="lbLast_Command">下一页</asp:LinkButton>
<asp:LinkButton ID="lbLast" runat="server" CommandName="last"OnCommand="lbLast_Command"> 尾 页 </asp:LinkButton> 直 接 跳 转 到<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中。
商家介绍:星梦云怎么样,星梦云好不好,资质齐全,IDC/ISP均有,从星梦云这边租的服务器均可以备案,属于一手资源,高防机柜、大带宽、高防IP业务,一手整C IP段,四川电信,星梦云专注四川高防服务器,成都服务器,雅安服务器,。活动优惠促销:1、成都电信夏日激情大宽带活动机(封锁UDP,不可解封):机房CPU内存硬盘带宽IP防护流量原价活动价开通方式成都电信优化线路2vCPU2G40G+60G21...
BuyVM 商家算是有一些年头,从早年提供低价便宜VPS主机深受广大网友抢购且也遭到吐槽的是因为审核账户太过于严格。毕竟我们国内的个人注册账户喜欢账户资料乱写,毕竟我们看英文信息有些还是比较难以识别的,于是就注册信息的时候随便打一些字符,这些是不能通过的。前几天,我们可以看到BUYVM商家有新增加迈阿密机房,而且商家有提供大硬盘且不限制流量的VPS主机,深受有一些网友的喜欢。目前,BUYVM商家有...
天上云服务器怎么样?天上云是国人商家,成都天上云网络科技有限公司,专注于香港、美国海外云服务器的产品,有多年的运维维护经验。世界这么大 靠谱最重,我们7*24H为您提供服务,贴心售后服务,安心、省事儿、稳定、靠谱。目前,天上云香港大带宽物理机服务器572元;20Mbps带宽!三网CN2线路,香港沙田数据中心!点击进入:天上云官方网站地址香港沙田数据中心!线路说明 :去程中国电信CN2 +中国联通+...