Repeater绑定数据库,使用AspNetPager进行分页
分页是Web中经常遇到的功能,分页主要有真分页和假分页。
所谓真分页是指:每一页显示多少数据,就从数据库读多少数据;
假分页是指:一次性从数据库读取所有数据,然后再进行分页。
这两种分页方式区别在于从数据库读取信息的方式,真分页的效率高。假分页在首次页面加载的时候会比较慢(如果数据量较多)。
下面学习下使用AspNetPager进行真分页
1.前台编写Repeater所呈现的数据:
<table width="" border="">
<tr>
<td class="tr1">
<asp:Label Text="姓名" runat="server"></asp:Label>
</td>
<td class="tr2">
<asp:Label Text="所在公司" runat="server"></asp:Label>
</td>
<td class="tr3">
<asp:Label Text="注册ID" runat="server"></asp:Label>
</td>
</tr>
</table> <asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table border="" width="">
<tr>
<td class="tr1">
<%#Eval("E_Id")%>
</td>
<td class="tr2">
<%#Eval("C_Id") %>
</td>
<td class="tr3">
<%#Eval("User_Id") %>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
aspx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAzIAAAC9CAIAAADnWd/qAAARxUlEQVR4nO3dS7IqxxUFUCI0MbU9E8KTuONyRzTVVMMDsMagARw3sOuW8kdCFpCXXCsqXkCRFOjp5GHXB94pAIBpfH19vfst8Dand78BAOCbWLay/8WyL3jI5XJRQmAiMG5fRa9PA0ziO5a9933wQ+kjECYCR1BFhFjGIH0EwkTgCKqIEMsYpI9AmAgcQRURxVh2Pp+TQfma/kf5bI0+khTG+XyulUpPCW1j7q039ckL+EBlnCoixmOZz7zF5X3k/H/Fu0V5gGuMqT3adud/FtynOBH2Ax4oQnW7GrGMuBnLrrfbsczn38oe+zS6maLaG7mrzNQkL1D8QO2s26TsddRliWVEEsvydvC8D0s+Q+No2c1Mv42/+So3C6+R7ZQlLzDygXr++5Hg5G78/ZCz0PbBxDIiP1qWNIJk5V6tg7CUxtGye5N9rYQa5ddDZfICyURICru2z7AN2N8oNtVap1Xen0QsI9qxbBuU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geX1/MNK9THJ4ZP24rmf/dzBDT5jZP9GetSu/WqMiSxdNU50dka017jrnOxsxDIAgCmc/vXPsFgsFovFYrG8fRHLLBaLxWKxWKZYTn/++R+LxWKxWCwWy9uX09vfgcVisVgsFovlzz//818MXTDNlUbl+wAAAABJRU5ErkJggg==" alt="" />
2.加入AspNetPager控件
<webdiyer:AspNetPager ID="AspNetPager1" runat="server"
AlwaysShow="true" //始终显示分页控件,即使至分成一页
UrlPaging="true" //通过URL传递分页信息
NumericButtonTextFormatString="[{0}]" //索引格式
ShowCustomInfoSection="Left" //显示当前页和总页数信息,默认值不显示,为left则将显示在页索引前,为right则为页索引后
ShowInputBox="Always" //输入框
TextAfterInputBox="页" //输入框之后
TextBeforeInputBox="跳转到第" > //输入框之前
</webdiyer:AspNetPager>
3.后台分页及绑定数据
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeater();
}
}
a.BindRepeater()函数,绑定数据库等操作
public void BindRepeater()
{
this.AspNetPager1.PageSize = ;//分页大小
int count = ;//当前页默认为1 if (Request.QueryString["page"] != null)//如果当前页非空,则将URL中的page赋值为当前页的值
{
count = Convert.ToInt32(Request.QueryString["page"].ToString());//使用URL传递分页信息,(如果使用AspNetPager的PageChanged函数,会在函数中调用两次PageChanged函数,影响运行效率)
}
int num = (count - ) * this.AspNetPager1.PageSize; //当前页减1,乘以每页显示数目,得到前几页的数据数量
string sql = "select top " + this.AspNetPager1.PageSize + " * from Emp where E_Id not in (" +
" select top " + num + " E_Id from Emp order by E_Id asc) order by E_Id asc";//自定义的SQL语句,查找当前页的数据
int recordcount;
DataSet ds = GetPage(sql, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out recordcount);
this.AspNetPager1.RecordCount = recordcount;
Repeater1.DataSource = ds;
Repeater1.DataBind();
AspNetPager1.CustomInfoHTML = "记录总数:<b>" + AspNetPager1.RecordCount.ToString() + "</b>";
AspNetPager1.CustomInfoHTML += " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>";
AspNetPager1.CustomInfoHTML += " 当前页:<font color=\"red\"><b>" + count + "</b></font>"; }
b.GetPage函数,返回数据集
/// <summary>
/// 获得数据源
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="currentPage">当前页</param>
/// <param name="pagesize">分页大小</param>
/// <param name="recordcount">总页数</param>
/// <returns>DataSet</returns>
public DataSet GetPage(string sql, int currentPage, int pagesize, out int recordcount)
{
// String strSql = "select * from Emp";
SqlDataAdapter ada = new SqlDataAdapter(sql, GetConnection());
DataSet ds = new DataSet();
//int startRow = (currentPage - 1) * pagesize;
//ada.Fill(ds, startRow, pagesize, "table");//对读取到的数据进行分页,假分页时可以这样操作
ada.Fill(ds, "table"); //填充
recordcount = GetPageRecord();//得到总页数
return ds;
}
c.GetPagRecord函数,获得总记录数
/// <summary>
/// 获得总记录数
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public int GetPageRecord()
{
String sql = "select count(*) from Emp";
SqlCommand cmd = new SqlCommand(sql, GetConnection());
cmd.Connection.Open();
int recordcount = (int)cmd.ExecuteScalar();
return recordcount;
}
d.GetConnection,获得连接串
public SqlConnection GetConnection()
{
SqlConnection conn = new SqlConnection("server=.;database=ComInfo;integrated security=true");
return conn;
}
Repeater绑定数据库,使用AspNetPager进行分页的更多相关文章
- Repeater绑定数据库
前台: <table width="> <tr> <td class="tr1"> <asp:Label Text=" ...
- 如何用visual studio控件(repeater)绑定数据库(SQL server)信息并显示
今天学习了下如何间接绑定数据库网上看了很多信息,都云里雾里,没有图片说明,初学者完全看不懂,我自己做了一个DEMO,相信可以帮到大家! 一.建立数据库,并构建表信息,我的表信息如下: 表中的数据在数据 ...
- SqlServer分页存储过程(多表查询,多条件排序),Repeater控件呈现数据以及分页
存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出 ...
- AspNetPager 免费分页控件7.5.1版发布!
AspNetPager 免费分页控件7.5.1版发布,本次升级主要内容有: 修正了ShowDisabledButtons为false时html闭合标签丢失的bug:改为从System.Web.UI.W ...
- TreeView树形控件递归绑定数据库里的数据
TreeView树形控件递归绑定数据库里的数据. 第一种:性能不好 第一步:数据库中查出来的表,字段名分别为UNAME(显示名称),DID(关联数据),UTYPE(类型) 第二步:前台代码 <% ...
- Repeater控件使用(含删除,分页功能)
Repeater控件使用(含删除,分页功能) 摘自:http://www.cnblogs.com/alanliu/archive/2008/02/25/914779.html 前臺代碼 <%@ ...
- comboBox绑定数据库、模糊查询
实现: 一.绑定数据库 点击查询按钮,comboBox显示从数据库查到的某字段的一列数据 方法:在按钮的点击事件绑定数据库 private void button1_Click(object send ...
- 理解asp.net中DropDownList编辑数据源,绑定数据库数据。
一.理解asp.net绑定数据库 终于学习到了连接数据库部分的内容,杨中科老师视频看起来挺轻松的,如果是高清版就更ok了. 我发现我学习新的编程语言会有一个特点,都想要赶紧学习数据库,数据就是一切,有 ...
- 【转】通用分页用户控件(DataGrid,DataList,Repeater都可以用它来分页)
通用分页控件(DataGrid,DataList,Repeater都可以用它来分页) 1.建立用户控件Pager.ascx 1.1 html </ASP:LABEL></TD> ...
随机推荐
- NGUI类之间的关系和架构
NGUI Drawcall 1.使用同一个altals的元素尽量放在同一个UIPanel下面,在NGUI中,它消耗的drawcall是以每个Panel为独立计算单位进行计算的. 2.如果一个UIPan ...
- HTML5 + CSS3 + JavaScript
http://www.programmer.com.cn/14761/#more-14761 文/李晶 随着互联网产业的爆炸式增长,与之伴生的Web前端技术也在历经洗礼和蜕变.尤其是近几年随着移动终端 ...
- android实现对导航Tab设置下划线选中效果
技术人员核心竞争力还是技术啊.努力提高各种实现效果.加油哦! 直接看效果.此linearLayout只有两个Button ,当选中Button1,Button1有个下划线选中效果.当选中Buton2, ...
- android 复杂的json数据解析
1.通过谷歌的Gson来进行解析: json数据:sTotalString = {"message":"success","result": ...
- AES的S-BOX构造优化
之前写过SBOX的构造,后来看到别人的优秀思路,借鉴过来重新改了一点. 原文地址:http://www.cnblogs.com/7hat/p/3383546.html 主要是将矩阵运算改为列运算之和, ...
- hadoop2.2.0的ha分布式集群搭建
hadoop2.2.0 ha集群搭建 使用的文件如下: jdk-6u45-linux-x64.bin hadoop-2.2.0.x86_64.tar zookeeper-3.4.5. ...
- java中的“包”与C#中的“命名空间
原文地址:http://www.cnblogs.com/lidabo/archive/2012/12/15/2819865.html Package vs. Namespace 我们知道,重用性(re ...
- 浅析NSTimer & CADisplayLink内存泄露
偶得前言 NSRunLoop与定时器 - invalidate的作用 我们如何解决? 偶得前言 本篇文章中我们主要谈谈NSTimer\CADisplayLink在使用过程中牵扯到内存泄露的相关问题及解 ...
- 单源最短路径(dijkstra算法)php实现
做一个医学项目,当中在病例评分时会用到单源最短路径的算法.单源最短路径的dijkstra算法的思路例如以下: 如果存在一条从i到j的最短路径(Vi.....Vk,Vj),Vk是Vj前面的一顶点.那么( ...
- 解密UML九中关系
将UML中经常使用的九种关系分为了四组进行解释. 一.组合和聚合解说: 同样:均是指有部分组成总体. 不同:聚合是指能够独立存在的个体组成总体.(弱的拥有关系) 组合存在时间上的关系.总体和部分具有同 ...