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 ...
随机推荐
- Could not find property 'outputFile
* What went wrong: A problem occurred configuring project ':app'. > Could not find property 'outp ...
- android studio签名
1.Build -> Generate Signed APK...,打开如下窗口 2.假设这里没有打过apk包,点击Create new,窗口如下 这里只要输入几个必要项 Key store p ...
- 认识 SurfaceView
SurfaceView是基于View视图进行扩展的视图类,适用于2D游戏开发,主要特点有: [1]surfaceView中对于画布的重绘是由一个新的线程去绘制,因此可以处理一些耗时的操作 [2]sur ...
- Ext.Net 1.x_Ext.Net.GridPanel之Access数据库分页显示
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- Linux 用户打开进程数的调整
Linux 用户打开进程数的调整 参考文章: 关于RHEL6中ulimit的nproc限制(http://www.cnblogs.com/kumulinux/archive/2012/12/16/28 ...
- STL算法设计理念 - 预定义函数对象
预定义函数对象基本概念:标准模板库STL提前定义了很多预定义函数对象 1)使用预定义函数对象: #include <iostream> #include <cstdio> #i ...
- Smarty学习笔记(二)
1.引用 {include file="xxx.xxx" sitename="xxx"} 向引入的文件传入变量: {include file="xxx ...
- 谈谈java中的线程(初级概念)
定义 关于进程与线程的定义 可参看一下这个介绍 http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html 在不细抠定义的情况下 ...
- C++多重继承与虚拟继承
本文只是粗浅讨论一下C++中的多重继承和虚拟继承. 多重继承中的构造函数和析构函数调用次序 我们先来看一下简单的例子: #include <iostream> using namespac ...
- SVN版本控制器中各符号的含义
SVN符号的含义 项目开发过程中,随着学习的不断深入,开始慢慢接触到版本管理控制工具,其实这个工具主要用于团队开发之中,但对于个人项目的备份也有好处,可以避免在电脑出现不可预知的故障时,最大化的保护自 ...