ASP.NET(C#) Repeater分页的实现

第一种方式:

数据库连接代码:
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;
using System.Data.SqlClient;
using System.Data.Sql;

public partial class _Default : System.Web.UI.Page
{
private void con()
{
SqlConnection con = new SqlConnection("server='.';database=test;uid=sa;pwd=123456;");
SqlConnection conn = new SqlConnection();
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("select * from news", con);
sda.Fill(ds, "newsTitle");
//SqlDataAdapter sda2 = new SqlDataAdapter("select * from ProspectiveBuyer", con);
// sda2.Fill(ds, "title");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["name"].DefaultView;
//PagedDataSource aa = new PagedDataSource();
pds.AllowPaging = true;//允许分页
pds.PageSize = 8;//单页显示项数
int CurPage;
if (Request.QueryString["Page"] != null)
CurPage = Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage = 1;
pds.CurrentPageIndex = CurPage - 1;
int Count = pds.PageCount;

lblCurrentPage.Text = "当前页:" + CurPage.ToString();
labPage.Text = Count.ToString();

if (!pds.IsFirstPage)
{
this.first.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
this.last.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(Count - 1); ;
up.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
}
else
{
this.first.Visible = false ;
this.last.Visible = false ;

}

if (!pds.IsLastPage)
{

next.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
}
else
{
this.first.Visible = false;
this.last.Visible = false;

}

Repeater1.DataSource = pds ;
Repeater1.DataBind();

}

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
con();
this.first.Visible = true;
this.last.Visible = true;
//this.Repeater1.DataSource = pds();
//this.Repeater1.DataBind();

}

}
}

aspx文件代码:
<table>
<tr ><td class="style1" align ="left" >hehe</td></tr>

<tr ><td class="style1">
<asp:Repeater ID="Repeater1" runat="server" >

<HeaderTemplate ><table><tr><td>头模板</td></tr></HeaderTemplate>
<ItemTemplate ><tr><td ><font color="red" > <%#("timekey")%></font></td></tr></ItemTemplate>
<AlternatingItemTemplate ><tr><td > <a href ='Default.aspx?id=<%#"databaselogid" %>'><%#("SalesAmountQuota")%></a></td></tr></AlternatingItemTemplate>
<FooterTemplate ><tr><td>尾模板</td></tr></table></FooterTemplate>
</asp:Repeater>
</td> </tr>

<tr> <td class="style1">
<asp:HyperLink ID="first" runat="server">首页</asp:HyperLink>
<asp:HyperLink ID="next" runat="server">下一页</asp:HyperLink>
<asp:HyperLink ID="up" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="last" runat="server">末页</asp:HyperLink>
</td></tr>

<tr> <td class="style1">当前页为:<asp:Label ID="lblCurrentPage" runat="server"
Text="Label"></asp:Label>&nbsp;
共<asp:Label ID="labPage" runat="server" Text="Label"></asp:Label>
页</td><td class="style1" style="height: 21px">
<asp:HyperLink ID="first" runat="server">首页</asp:HyperLink>

<asp:HyperLink ID="up" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="next" runat="server">下一页</asp:HyperLink>
<asp:HyperLink ID="last" runat="server">末页</asp:HyperLink>
</td></tr>

</table>
第二种方式:
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.Data.SqlClient;

public partial class databind : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
num.Text = "1";
repdatabind();
}

}
public void repdatabind()
{

SqlConnection con = new SqlConnection("server='.';database=test;uid=sa;pwd=123456;");
SqlConnection conn = new SqlConnection();
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("select * from DimProduct", con);
sda.Fill(ds, "name");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["name"].DefaultView;
pds.AllowPaging = true;//允许分页
pds.PageSize = 8;//单页显示项数

int curpage = Convert.ToInt32(num.Text);
this.BtnDown.Enabled = true;
this.BtnUp.Enabled = true;
pds.CurrentPageIndex = curpage - 1;
if (curpage == 1)
{
this.BtnUp.Enabled = false;
}
if (curpage == pds.PageCount)
{
this.BtnDown.Enabled = false;
}
this.Repeater1.DataSource = pds;
this.Repeater1.DataBind();
}

protected void BtnUp_Click(object sender, EventArgs e)
{
this.num.Text =Convert.ToString ( Convert.ToInt32(num.Text)- 1) ;
repdatabind();
}
protected void BtnDown_Click(object sender, EventArgs e)
{
this.num.Text = Convert.ToString(Convert.ToInt32(num.Text)+ 1) ;
repdatabind();
}
}
aspx代码:
<%@ Page Language="C#" CodeFile="databind.aspx.cs" Inherits="databind" %>

<!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:Panel ID="Panel1" runat="server" Height="173px">
<asp:Repeater ID="Repeater1" runat="server"><HeaderTemplate ><table border onmousedown="1" ><tr><td >头模板</td></tr></HeaderTemplate><ItemTemplate ><tr><td>序号:<%# ("ProductKey") %></td></tr><tr><td>编码:<%# ("ProductAlternateKey") %></td></tr></ItemTemplate><FooterTemplate
><tr><td>脚模板</td></tr></table></FooterTemplate>
</asp:Repeater>
当前页:<asp:Label ID="num" runat="server"></asp:Label>
<br />
<asp:Button ID="BtnUp" runat="server" onclick="BtnUp_Click" Text="上一页" />
<asp:Button ID="BtnDown" runat="server" onclick="BtnDown_Click" Text="下一页" />
</asp:Panel>
<br />
<br />

</div>
</form>
</body>
</html>

文章出处:http://www.cnblogs.com/rond/articles/1888241.html

ASP.NET(C#) Repeater分页的实现的更多相关文章

  1. asp.net中Repeater控件用法笔记

    大家可能都对datagrid比较熟悉,但是如果在数据量大的时候,我们就得考虑使用 repeater作为我们的数据绑定控件了.Repeater控件与DataGrid (以及DataList)控件的主要区 ...

  2. ASP利用Recordset实现分页

    <!--#INCLUDE FILE="../function/db.asp" --> <!--#INCLUDE FILE="../function/co ...

  3. 学习ASP.NET MVC(十一)——分页

    在这一篇文章中,我们将学习如何在MVC页面中实现分页的方法.分页功能是一个非常实用,常用的功能,当数据量过多的时候,必然要使用分页.在今天这篇文章中,我们学习如果在MVC页面中使用PagedList. ...

  4. asp.net利用存储过程分页代码

    -最通用的分页存储过程 -- 获取指定页的数据 CREATE PROCEDURE Pagination ), -- 表名 ) = '*', -- 需要返回的列 )='', -- 排序的字段名 , -- ...

  5. ASP.NET MVC 简单分页代码

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  6. Asp 解析 XML并分页显示

    Asp 解析 XML并分页显示 Asp 解析 XML并分页显示,演示样例源代码例如以下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...

  7. asp.net MVC通用分页组件 使用方便 通用性强

    asp.net MVC通用分页组件 使用方便 通用性强   该分页控件的显示逻辑: 1 当前页面反色突出显示,链接不可点击 2 第一页时首页链接不可点击 3 最后一页时尾页链接不可点击 4 当前页面左 ...

  8. Asp.net MVC 简单分页 自做简单分页

    Asp.net MVC 简单分页:   public static string Pager(int page,int pageSize,int total)         {           ...

  9. 自己写的一个ASP.NET服务器控件Repeater和GridView分页类

    不墨迹,直接上代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...

随机推荐

  1. 敏捷测试(2)--ATDD概念

    什么是验收测试驱动开发 在准备实施一个功能或特性之前,首先团队需要定义出期望的质量标准和验收细则,以明确而且达成共识的验收测试计划(包含一系列测试场景)来驱动开发人员的TDD实践和测试人员的测试脚本开 ...

  2. 传输控制协议(TCP) -- TCP状态转换图

    TCP状态转换图 在<UNIX网络编程 卷1>一书中,作者给出了TCP状态转换图(如下).本文也将围绕此图进行阐释. 注:上图红框表示比较特殊的地方. TCP状态转换两条主线 图2-4中的 ...

  3. C++ 传参时传内置类型时用传值(pass by value)方式效率较高

    来源:唐磊的个人博客<C++ 传参时传内置类型时用传值(pass by value)方式效率较高> 在<Effective C++>里提到对内置(C-like)类型在函数传参时 ...

  4. 数据库用户映射到SQL Server登录名

    由于不是固定在一如电脑开始,经常需要把数据备份与恢复.或者是帮助客户修改程序,接收到客户备份好的数据,需要在电脑恢复. 这样就需要将数据库用户映射到 SQL Server 登录名.如何映射?可使用下面 ...

  5. RubyGem默认源安装太慢,修改国内淘宝源

    原帖地址:http://www.hiceon.com/topic/Ruby-Gem-install-source-taobao/ WHY? 由于国内网络原因(你懂的),导致 rubygems.org ...

  6. 如何设计一个可用的web容器

    之前在另外一个平台(http://www.jointforce.com/jfperiodical/article/1035)发表的一篇文章,现在发布到自己的博客上. 开发一个web容器涉及很多不同方面 ...

  7. git push 小结

    $ git push ssh://git@dev.lemote.com/rt4ls.git master // 把本地仓库提交到远程仓库的master分支中 $ git remote add orig ...

  8. Jmeter(二十五)_Xpath关联

    在Jmeter中,除了正则表达式可以用作关联,还有一种方式也可以做关联,那就是 XPath Extractor.它是利用xpath提取出关键信息,传递变量. 具体用法 添加一个后置处理器-XPath ...

  9. contentType,charset和pageEncoding的区别

    简单点总结就是jsp页面头上这样写  <%@ page contentType="text/html;charset=GBK" %> 页面用GBK编码 pageEnco ...

  10. 学习MQ(一) 感知

    声明:我的文字里出现的MQ,如没有特殊指明,就是指的IBM的websphere MQ 以前对MQ一无所知! MQ是IBM websphere的系列产品之一,是很好的一个中间件产品.其实我对这列产品并不 ...