先创建一个Paging类用来创造分页对象
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
/// <summary>
///分页数据源类
/// </summary>
public class Paging
{
protected string _connStr;
private int _pageSize;
/// <summary>
/// 用每页显示的记录条数初始化Paging对象
/// </summary>
/// <param name="pageSize">每页显示的记录条数</param>
public Paging(int pageSize)
{
_connStr = ConfigurationManager.ConnectionStrings["QQConnectionString"].ConnectionString;
_pageSize = pageSize;
}
// 根据指定的SQL语句返回一张数据表
private DataTable GetData(string cmdText)
{
SqlConnection conn = new SqlConnection(_connStr);
DataSet dt = new DataSet();
try
{
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmdText, conn);
adapter.Fill(dt);
}
finally
{
conn.Close();
}
return dt.Tables[0];
}
/// <summary>
/// 返回一个分页数据源,未指定当前页码
/// </summary>
public PagedDataSource GetPageDataSource(string cmdText)
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = GetData(cmdText).DefaultView;
pds.AllowPaging = true;
pds.PageSize = _pageSize;
pds.CurrentPageIndex = pds.CurrentPageIndex;
return pds;
}
/// <summary>
/// 根据传来的整数作为当前页码,返回一个分页数据源
/// </summary>
public PagedDataSource GetPageDataSource(string cmdText, int page)
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = GetData(cmdText).DefaultView;
pds.AllowPaging = true;
pds.PageSize = _pageSize;
if (page < 1) page = 1;
if (page > pds.Count) page = pds.Count;
pds.CurrentPageIndex = page - 1; ;
return pds;
}
public static int Execute(string cmdText)
{
string connStr = ConfigurationManager.ConnectionStrings["QQConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(cmdText, conn);
return cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
}
下面是绑定操作。要在DataList控件的EditItemTemplate和ItemTemplate里添加LinkButton控件才能用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class lianxi_datalist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int n;
string page = Request.QueryString["page"];
if (!string.IsNullOrEmpty(page))
{
n = Convert.ToInt32(page); // 可能不在页数范围内
}
else
{
n = 1;
}
Bind(n);
}
}
// 用当前的页码进行绑定。因为更新DataList的项后会跳到第一页,这个方法让DataList停在当前页
public void Bind()
{
Paging page = new Paging(20);
int n = Convert.ToInt32(Request.QueryString["page"]);
DataList1.DataSource = page.GetPageDataSource("select * from [BaseInfo]", n);
DataList1.DataKeyField = "QQID"; // 把QQID存到数据列表的DataKeys中
DataList1.DataBind();
}
// 用指定的页码进行绑定
public void Bind(int n)
{
Paging page = new Paging(20);
DataList1.DataSource = page.GetPageDataSource("select * from [BaseInfo]", n);
DataList1.DataKeyField = "QQID";
DataList1.DataBind();
}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
Bind();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
Bind();
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string qq = DataList1.DataKeys[e.Item.ItemIndex].ToString();
string cmdText = "delete from [BaseInfo] where QQID=" + Convert.ToInt64(qq);
try
{
if (Paging.Execute(cmdText) > 0)
{
Bind();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string qq = DataList1.DataKeys[e.Item.ItemIndex].ToString();
string nickname = ((TextBox)e.Item.FindControl("txtNickName")).Text;
string address = ((TextBox)e.Item.FindControl("txtAddress")).Text;
string phone = ((TextBox)e.Item.FindControl("txtPhone")).Text;
string cmdText = "update baseinfo set NickName='" + nickname + "',Address='" + address + "',Phone='" + phone + "' where QQID='" + qq + "'";
try
{
if (Paging.Execute(cmdText) > 0)
{
DataList1.EditItemIndex = -1;
Bind();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
PagedDataSource pds = (PagedDataSource)DataList1.DataSource;
((Label)e.Item.FindControl("lblTotal")).Text = pds.Count.ToString();
((Label)e.Item.FindControl("lblCurrent")).Text = (pds.CurrentPageIndex + 1).ToString();
HyperLink lnkHome = ((HyperLink)e.Item.FindControl("lnkHome"));
lnkHome.NavigateUrl = "?page=1";
HyperLink lnkLast = ((HyperLink)e.Item.FindControl("lnkLast"));
lnkLast.NavigateUrl = "?page=" + pds.Count;
HyperLink lnkPrev = ((HyperLink)e.Item.FindControl("lnkPrev"));
HyperLink lnkNext = ((HyperLink)e.Item.FindControl("lnkNext"));
if (pds.CurrentPageIndex <= 0)
{
lnkHome.Enabled = false;
lnkPrev.Enabled = false;
}
else
{
lnkPrev.NavigateUrl = "?page=" + pds.CurrentPageIndex;
}
if (pds.CurrentPageIndex == pds.Count - 1)
{
lnkNext.Enabled = false;
lnkLast.Enabled = false;
}
else
{
lnkNext.NavigateUrl = "?page=" + (pds.CurrentPageIndex + 2);
}
}
}
}