DataList控件分页
Code
1<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Repeater._Default" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4<html xmlns="http://www.w3.org/1999/xhtml">
5<head runat="server">
6 <title>无标题页</title>
7</head>
8<body>
9 <form id="form1" runat="server">
10 <asp:DataList ID="DataList1" runat="server">
11 <HeaderTemplate>
12 <table>
13 </HeaderTemplate>
14 <ItemTemplate>
15 <tr>
16 <td>
17 序号:<%# Eval("DiscountID")%>
18 </td>
19 </tr>
20 <tr>
21 <td>
22 名称:<%# Eval("CompanyCode")%>
23 </td>
24 </tr>
25 </ItemTemplate>
26 <FooterTemplate>
27 </table>
28 </FooterTemplate>
29 </asp:DataList>
30 <br />
31 当前页:<asp:Label ID="currentpage" runat="server"></asp:Label>
32 <asp:Button ID="BtnUp" runat="server" OnClick="BtnUp_Click" Text="上一页" />
33 <asp:Button ID="BtnDown" runat="server" OnClick="BtnDown_Click" Text="下一页" />
34 </form>
35</body>
36</html>
37
1<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Repeater._Default" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4<html xmlns="http://www.w3.org/1999/xhtml">
5<head runat="server">
6 <title>无标题页</title>
7</head>
8<body>
9 <form id="form1" runat="server">
10 <asp:DataList ID="DataList1" runat="server">
11 <HeaderTemplate>
12 <table>
13 </HeaderTemplate>
14 <ItemTemplate>
15 <tr>
16 <td>
17 序号:<%# Eval("DiscountID")%>
18 </td>
19 </tr>
20 <tr>
21 <td>
22 名称:<%# Eval("CompanyCode")%>
23 </td>
24 </tr>
25 </ItemTemplate>
26 <FooterTemplate>
27 </table>
28 </FooterTemplate>
29 </asp:DataList>
30 <br />
31 当前页:<asp:Label ID="currentpage" runat="server"></asp:Label>
32 <asp:Button ID="BtnUp" runat="server" OnClick="BtnUp_Click" Text="上一页" />
33 <asp:Button ID="BtnDown" runat="server" OnClick="BtnDown_Click" Text="下一页" />
34 </form>
35</body>
36</html>
37
后台代码
Code
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;
namespace Repeater
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
currentpage.Text = "1"; //首次加载,把目前页置1
repdatabind(); //开始数据绑定
}
}
private void repdatabind()
{
//初始化数据连接
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
con.Open();
//定义数据集并填充要操作的数据表video
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM dbo.Discount", con);
sda.Fill(ds, "Discount");
//引入分页控件并定义分页属性
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["Discount"].DefaultView;//定义分页控件的数据源
pds.AllowPaging = true; //允许分页
pds.PageSize = 4; //单页显示项数
pds.CurrentPageIndex = Convert.ToInt32(currentpage.Text) - 1; //分页控件的分页索引
this.BtnDown.Enabled = true; //翻页按钮在每一次数据绑定前默认是激活的状态
this.BtnUp.Enabled = true;
if (pds.IsFirstPage) //初始化页面时“上一页”按钮为灰显
{
this.BtnUp.Enabled = false;
}
if (pds.IsLastPage) //如果当前页等于分页控件的页数 则“下一页”灰显
{
this.BtnDown.Enabled = false;
}
this.DataList1.DataSource = pds; //绑定数据显示控件到分页控件
this.DataList1.DataBind();
}
//下一页
protected void BtnDown_Click(object sender, EventArgs e)
{
this.currentpage.Text = Convert.ToString(Convert.ToInt32(currentpage.Text) + 1);
repdatabind();
}
//上一页
protected void BtnUp_Click(object sender, EventArgs e)
{
this.currentpage.Text = Convert.ToString(Convert.ToInt32(currentpage.Text) - 1);
repdatabind();
}
}
}
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;
namespace Repeater
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
currentpage.Text = "1"; //首次加载,把目前页置1
repdatabind(); //开始数据绑定
}
}
private void repdatabind()
{
//初始化数据连接
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
con.Open();
//定义数据集并填充要操作的数据表video
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM dbo.Discount", con);
sda.Fill(ds, "Discount");
//引入分页控件并定义分页属性
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["Discount"].DefaultView;//定义分页控件的数据源
pds.AllowPaging = true; //允许分页
pds.PageSize = 4; //单页显示项数
pds.CurrentPageIndex = Convert.ToInt32(currentpage.Text) - 1; //分页控件的分页索引
this.BtnDown.Enabled = true; //翻页按钮在每一次数据绑定前默认是激活的状态
this.BtnUp.Enabled = true;
if (pds.IsFirstPage) //初始化页面时“上一页”按钮为灰显
{
this.BtnUp.Enabled = false;
}
if (pds.IsLastPage) //如果当前页等于分页控件的页数 则“下一页”灰显
{
this.BtnDown.Enabled = false;
}
this.DataList1.DataSource = pds; //绑定数据显示控件到分页控件
this.DataList1.DataBind();
}
//下一页
protected void BtnDown_Click(object sender, EventArgs e)
{
this.currentpage.Text = Convert.ToString(Convert.ToInt32(currentpage.Text) + 1);
repdatabind();
}
//上一页
protected void BtnUp_Click(object sender, EventArgs e)
{
this.currentpage.Text = Convert.ToString(Convert.ToInt32(currentpage.Text) - 1);
repdatabind();
}
}
}
web.config 配置
Code
1 <connectionStrings>
2 <add name="db" connectionString="Data Source=192.168.1.101;Initial Catalog=test;persist security info=False;user id=sa;pwd=sa;" providerName="System.Data.SqlClient"/>
3 </connectionStrings>
1 <connectionStrings>
2 <add name="db" connectionString="Data Source=192.168.1.101;Initial Catalog=test;persist security info=False;user id=sa;pwd=sa;" providerName="System.Data.SqlClient"/>
3 </connectionStrings>
下面是限制前台用户输入的页数是否有效
Code
1 // 计算页码的范围 ,一般使用out 参数取得总记录数,sumpage 是总记录数,size 是要显示页数 ,取出总共有多少页
2 int PageCount = (sumpage + Size - 1) / Size;
3
4 //使用ASP.NET 自带的验证控件
5
6 this.RangeValidator1.MinimumValue = "1";
7 this.RangeValidator1.MaximumValue = PageCount.ToString();
8
1 // 计算页码的范围 ,一般使用out 参数取得总记录数,sumpage 是总记录数,size 是要显示页数 ,取出总共有多少页
2 int PageCount = (sumpage + Size - 1) / Size;
3
4 //使用ASP.NET 自带的验证控件
5
6 this.RangeValidator1.MinimumValue = "1";
7 this.RangeValidator1.MaximumValue = PageCount.ToString();
8
出处:http://www.cnblogs.com/liuyong/
作者喜欢研究 Sql Server ,ASP.NET MVC , Jquery WCF 等技术,同时关心分布式架构的设计应用。转载请保留原文链接,谢谢!