datalist 分页
Asp.net提供了三个功能强大的列表控件:GridView、DataList和Repeater控件,相对GridView,DataList和Repeater控件具有更高的样式自定义性,很多时候我们喜欢使用DataList或Repeater控件来显示数据,但是Repeater和DataList没有分页功能,有时很不方便。
PagedDataSource类封装了GridView控件的属性,从而使GridView控件可以执行分页,它就是一个数据的容器,我们先把数据从数据库中读取出来放在这个容器中,然后设置容器的属性取出当前要显示的页上的部分数据,然后将此部分数据再绑定到页面上的显示控件上。
下面实例是用PagedDataSource类实现DataList控件的数据分页
页面后台代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;
using System.Data.SqlClient;
public partial class Demo : System.Web.UI.Page
{
protected SqlConnection conn; //添加数据库的操作对象
protected SqlDataAdapter da;
protected DataSet ds;
protected SqlCommand comm;
protected void Page_Load(object sender, EventArgs e)
{
getArticle();
}
private void getArticle() //取得Article数据
{
conn = new SqlConnection("server=127.0.0.1;database=ObtainEmployment;user id=sa;password=;");//取连接字符串,建立连接
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("SELECT top 50 * FROM db_Article where checkup='1' ORDER BY intime DESC ", conn);
ds = new DataSet();
try
{
conn.Open();
da.Fill(ds, "Article");
conn.Close();
}
catch (SqlException e1)
{
Response.Write(e1.ToString());
}
int cup = Convert.ToInt32(this.lb_CurrentPage.Text); //当前页数,初始化为地1页
PagedDataSource ps = new PagedDataSource();
ps.DataSource = ds.Tables["Article"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 6; //每页显示的数据的行数
ps.CurrentPageIndex = cup - 1;
lb_count.Text = ps.DataSourceCount.ToString(); //获取记录总数
lb_page.Text = ps.PageCount.ToString(); //获取总页数
if (!IsPostBack)
{
for (int i = 1; i < ps.PageCount + 1; i++)
{
this.DropDownList1.Items.Add(i.ToString());
}
LinkUp.Enabled = true;
LinkDown.Enabled = true;
}
try
{
DropDownList1.SelectedItem.Text = cup.ToString();
DataList1.DataSource = ps;
DataList1.DataBind();
}
catch (Exception ex)
{
throw ex;
}
}
protected void LinkDown_Click(object sender, EventArgs e) //下一页按钮代码
{
try
{
lb_CurrentPage.Text = Convert.ToString(Convert.ToInt32(lb_CurrentPage.Text) + 1);
DropDownList1.SelectedValue = lb_CurrentPage.Text;
getArticle();
}
catch (Exception ex)
{
Response.Write("<script language=javascript>" + "alert(/"已经是最后一页/")" + "</script>");
lb_CurrentPage.Text = "1";
getArticle();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) //跳转到指定页代码
{
int page =Convert.ToInt16((DropDownList1.SelectedItem.Value));
lb_CurrentPage.Text = page.ToString();
getArticle();
}
protected void LinkUp_Click(object sender, EventArgs e) //上一页按钮代码
{
try
{
if (Convert.ToInt16(lb_CurrentPage.Text) > 1)
{
lb_CurrentPage.Text = Convert.ToString(Convert.ToInt32(lb_CurrentPage.Text) - 1);
DropDownList1.SelectedValue = lb_CurrentPage.Text;
getArticle();
}
else
{
Response.Write("<script language=javascript>" + "alert(/"已经是第一页/")" + "</script>");
}
}
catch (Exception ex)
{
Response.Write("<script language=javascript>" + "alert(/"已经是第一页/")" + "</script>");
}
}
protected void LinkFirst_Click(object sender, EventArgs e) //跳到第一页代码
{
if (lb_CurrentPage.Text != "1")
lb_CurrentPage.Text = "1";
else
{
Response.Write("<script language=javascript>" + "alert(/"已经是第一页/")" + "</script>");
}
getArticle();
}
protected void LinkLast_Click(object sender, EventArgs e) //跳到最后一页代码
{
if (lb_CurrentPage.Text.ToString() !=lb_page.Text.ToString())
lb_CurrentPage.Text = lb_page.Text.ToString();
else
{
Response.Write("<script language=javascript>" + "alert(/"已经是最后一页/")" + "</script>");
}
getArticle();
}
}
页面前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Demo.aspx.cs" Inherits="Demo" %>
<!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:DataList ID="DataList1" runat="server">
<ItemTemplate>
<asp:Label ID="lbNwes" runat="server" Text='<%#Eval("title")%>'></asp:Label>
<asp:Label ID="lbTime" runat="server" Text='<%#Eval("intime")%>'></asp:Label>
</ItemTemplate>
</asp:DataList></div>
<br />
共<asp:Label ID="lb_count" runat="server" Text="Label"></asp:Label>条记录
共<asp:Label ID="lb_page" runat="server" Text="Label"></asp:Label>页
当前第<asp:Label ID="lb_CurrentPage" runat="server" Text="1"></asp:Label>页
<asp:LinkButton ID="LinkFirst" runat="server" OnClick="LinkFirst_Click">第一页</asp:LinkButton>
<asp:LinkButton ID="LinkUp" runat="server" OnClick="LinkUp_Click">上一页</asp:LinkButton>
<asp:LinkButton ID="LinkDown" runat="server" OnClick="LinkDown_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="LinkLast" runat="server" OnClick="LinkLast_Click">最后一页</asp:LinkButton>
转到第<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>页
</form>
</body>
</html>
注: PagedDataSource 类的部分公共属性:
AllowCustomPaging 获取或设置指示是否启用自定义分页的值。
AllowPaging 获取或设置指示是否启用分页的值。
Count 获取要从数据源使用的项数。
CurrentPageIndex 获取或设置当前页的索引。
DataSource 获取或设置数据源。
DataSourceCount 获取数据源中的项数。
FirstIndexInPage 获取页中的第一个索引。
IsCustomPagingEnabled 获取一个值,该值指示是否启用自定义分页。
IsFirstPage 获取一个值,该值指示当前页是否是首页。
IsLastPage 获取一个值,该值指示当前页是否是最后一页。
IsPagingEnabled 获取一个值,该值指示是否启用分页。
IsReadOnly 获取一个值,该值指示数据源是否是只读的。
IsSynchronized 获取一个值,该值指示是否同步对数据源的访问(线程安全)。
PageCount 获取显示数据源中的所有项所需要的总页数。
PageSize 获取或设置要在单页上显示的项数。
VirtualCount 获取或设置在使用自定义分页时数据源中的实际项数。
datalist 分页的更多相关文章
- DataList分页访问FooterTemplate模板里的控件
今天做DataList分页的时候,突然想把分页控件写在FooterTemplate模板里面,弄了很久都访问不到控件,终于发现问题所在,以下是访问FooterTemplate里控件的方法: <Fo ...
- 《ASP.NET1200例》<asp:DataList>分页显示图片
aspx页面代码 <asp:DataList ID="dlPhoto" runat="server" Height="137px" W ...
- 使用DataList 分页方法
什么是DataList我想应该不需要解释了,接下来分享本人在项目里使用到的通过DataList进行分页展示方法. 首先在ASPX页面添加一个DataList(后面都简称DL)控件,示例代码如下: &l ...
- DataList分页-增加自动编号列
<asp:DataList ID="dl_XUDAXIA" runat="server"> <HeaderTemplate> <t ...
- (转)实现DataList的分页 新增列
前几天在做网上商城,要展示商品信息(有图片,有文字),DataView虽然可以分页,但它的缺点是不能自定义显示格式.而DataList解决了它的缺点,但DataList本身却不能分页.很是头痛,于是在 ...
- ASP.NET Repeater控件实现简单分页
早上,有看MSDN,看到了 PagedDataSource 类 http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.pa ...
- MySQL+Service+Servlet+Jsp实现Table表格分页展示数据
下面以一个示例讲解如何使用MySQL+Service+Servlet+Jsp实现Table表格分页展示数据: eg:请假管理系统 要求如下: 一.打开首页页面, 访问查询请假记录的 servlet , ...
- phongap+ jquery + asp.net +android,我把我遇到的问题和处理方法的连接总结一下
这些都是最基本的问题,在实际的运用中都会用到 第1章.搭建Android的开发环境-跟我学编程 Win7旗舰版中的IIS配置asp.net的运行环境 - 追夢 - 博客园 vs2012下asp.net ...
- Insus Paging Utility Version 2
Insus.NET对GridView或是DataList分页,都是使用自己的分页组件:http://www.cnblogs.com/insus/archive/2009/03/19/1417102.h ...
随机推荐
- 在IFrame中查找IFRAME中的元素的方式
下面是内部iframe找外部mainFrame的情况 var websiteSearchButton = window.parent.parent.document.getElementById(' ...
- 教你自己写Android第三方库
其实Android studio的出现很大程度上方便了我们Android开发人员,今天我们说说怎么构建我们自己的库. 依次按File->New Moudle->android Librar ...
- JSONP获取Twitter和Facebook文章数
原文链接: Retrieve Twitter and Facebook Counts with JSON 翻译人员: 铁锚 原文日期: 2014年02月19日 翻译日期: 2014年02月22日 !! ...
- laydate日期空间与时间选择器
http://laydate.layui.com/
- ERP-非财务人员的财务培训教(一.二)------财务基础知识
二.基本财务管理知识 第一节 财务管理基础知识(一) 财务与会计的关系 会计的基础知识 (一) 财务与会计的关系 财务与会计的内涵 1.会计 会计工作主要是解决三个环节的问题: 会计凭证 会计账簿 会 ...
- android 开发从入门到精通
Android-Tips This is an awesome list of tips for android. If you are a beginner, this list will be t ...
- PS 滤镜算法原理——碎片效果
%%% Fragment %%% 对原图做四个方向的平移,然后对平移的结果取平均 %%% 碎片效果 clc; clear all; Image=imread('4.jpg'); Image=doubl ...
- iOS监听模式系列之键值编码KVC、键值监听KVO的简单介绍和应用
键值编码KVC 我们知道在C#中可以通过反射读写一个对象的属性,有时候这种方式特别方便,因为你可以利用字符串的方式去动态控制一个对象.其实由于ObjC的语言特性,你根部不必进行任何操作就可以进行属性的 ...
- 【55】java异常机制剖析
一.为什么要使用异常 首先我们可以明确一点就是异常的处理机制可以确保我们程序的健壮性,提高系统可用率.虽然我们不是特别喜欢看到它,但是我们不能不承认它的地位,作用.有异常就说明程序存在问题,有助于我们 ...
- iOS 字体权重weight
UIFontWeightUltraLight - 超细字体 UIFontWeightThin - 纤细字体 UIFontWeightLight - 亮字体 UIFontWeightRegular ...