datalist怎么样实现分页功能.

datalis实现分页的代码 精简一点就可以, 谢谢.
2024年11月30日 09:44
有2个网友回答
网友(1):

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.SqlClient;

public partial class Demo : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
dlBind();
}

public void dlBind()
{
int curpage = Convert.ToInt32(lb_CurrentPage.Text);
SqlConnection conn = new SqlConnection("server=.;database=db_MyData;uid=sa;pwd=");
conn.Open();
SqlDataAdapter da = new SqlDataAdapter("select top 50 * from tb_user", conn);
DataSet ds = new DataSet();
da.Fill(ds, "tb_user");
PagedDataSource ps = new PagedDataSource();
ps.DataSource = ds.Tables["tb_user"].DefaultView;
ps.AllowPaging = true; //是否分页
ps.PageSize = 5; //显示的数量
ps.CurrentPageIndex = curpage - 1; //取得当前页的页码
if (!IsPostBack)
{
for (int i = 1; i <= ps.PageCount; i++)
{
DropDownList1.Items.Add(i.ToString());
}
DropDownList1.SelectedItem.Text = curpage.ToString();
}
LinkFirst.Enabled = true;
LinkUp.Enabled = true;
LinkDown.Enabled = true;
LinkLast.Enabled = true;
if (curpage == 1)
{
LinkFirst.Enabled = false; //不显示第一页按钮
LinkUp.Enabled = false; //不显示上一页按钮
}
if (curpage == ps.PageCount)
{
LinkDown.Enabled = false; //不显示下一页按钮
LinkLast.Enabled = false; //不显示最后一页按钮
}
lb_page.Text = Convert.ToString (ps.PageCount); //获取总页数
lb_count .Text =ps .DataSourceCount .ToString (); //获取记录总数
DataList1.DataSource = ps;
DataList1.DataKeyField = "userLoginID";
DataList1.DataBind();
}

protected void LinkFirst_Click(object sender, EventArgs e) //第一页按钮
{
lb_CurrentPage.Text = "1";
dlBind();
}

protected void LinkUp_Click(object sender, EventArgs e) //上一页按钮
{
lb_CurrentPage.Text = Convert.ToString(Convert .ToInt32 (lb_CurrentPage .Text )-1);
dlBind();
}
protected void LinkDown_Click(object sender, EventArgs e) //下一页按钮
{
lb_CurrentPage .Text =Convert .ToString (Convert .ToInt32 (lb_CurrentPage .Text) +1);
dlBind ();
}
protected void LinkLast_Click(object sender, EventArgs e) //最后一页按钮
{
lb_CurrentPage.Text = lb_page.Text;
dlBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) //跳转到指定页
{
int page = Convert.ToInt32(DropDownList1.SelectedItem.Value);
lb_CurrentPage.Text = page.ToString();
dlBind();
}
}

网友(2):

如是。net的话,会有专门的分页属性。去datalist的属性里面找