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. iOS-导航头像缩放,支持点击回调

    在很多App中,经常存在一种需求就是,界面上下滚动时用户的头像也会跟着滚动,而用户头像在视图向上滚动一定范围时停留并在导航栏的位置 基本用法如下:1.单纯的实现这一效果: - (LEOHeaderVi ...

  2. 基于Struts+Hibernate开发过程中遇到的错误

    1.import  javax.servlet.http.HttpServletRequest 导入包出错 导入包出错,通常是包未引入,HttpServletRequest包是浏览器通过http发出的 ...

  3. Helix Streaming Server 简单配置

    双击桌面上新出现的"HelixServer"图标,正常的话应该如图9,不要关闭这个窗口. 双击"HelixServerAdministrator"图标,输入用户 ...

  4. 《java入门第一季》StringBuffer类小案例

    /* * 把数组拼接成一个字符串 */ public class StringBufferTest2 { public static void main(String[] args) { // 定义一 ...

  5. AndroidBinder进程间通信系统-android学习之旅(86)

    目录 前言及知识准备 Service组件结构 Clinet组件结构 与Binder驱动程序交互 总结 Binder进程间通信实例 问题 本次主要介绍Android平台下Binder进程间通信库.所谓通 ...

  6. Bootstrap 简介: 创建响应式、移动项目的工具

    原文链接: Introduction to Bootstrap: A Tool for Building Responsive, Mobile-First Projects 下载: 示例代码Boots ...

  7. linux下播放组播流出现setsockopt:No such device错误

    在linux下播放组播流出现setsockopt:No such device错误是因为多播IP没有add路由表里面 可以采用如下命令完成: root@android:/ # busybox rout ...

  8. VirtualBox安装RHEL之后配置桥接网络

    VirtualBox安装RHEL之后配置桥接网络 1 如果主机是Intel (R) Ethernet Connection I217-LM上网的: 2 如果主机是无线上网的, 如ipconfig显示如 ...

  9. DEVICE_ATTR实例分析

    在内核中, sysfs 属性一般是由 __ATTR 系列的宏来声明的,如对设备的使用 DEVICE_ATTR ,对总线使用 BUS_ATTR ,对驱动使用 DRIVER_ATTR ,对类别(class ...

  10. SpriteBuilder中的CCB Node尺寸

    当你创建一个类型为Layer的CCB文件时,你将注意到它的默认尺寸大小为568x384. 568个点是4英寸iphone的宽度,同时iPad屏幕只有512个点宽,更准确的说--SpriteBuilde ...