datalist分页(一)控件通用
全了 啊
其实 下面的内容 也是girdview/datalist/repeater都可以用,只需要改一下控件名 DataList1/Girdview1/Repeater.DataSource = ps;
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 test : 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=.;database=test;user id=sa;password=123;");//取连接字符串,建立连接
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select NewsId,NewsTitle,NewsContents,CreateDate from tblNews order by CreateDate 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>");
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();
}
}