我在 asp.net2.0 中使用了 aspnetpager 分页控件,我没有用到存储过程分页,只是用了 sql 语句进行的分页。
方法是这样的:
public static DataSet ExecuteWebDiyerDataset(string connString, CommandType cmdType, string cmdText, int mypageindex, int mypagesize, params SqlParameter[] cmdParms)
{
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connString))
{
PrepareCommand(myAdapter.SelectCommand, conn, null, cmdType, cmdText, cmdParms);
DataSet ds = new DataSet();
myAdapter.Fill(ds, mypagesize * (mypageindex - 1), mypagesize, "dtable");
myAdapter.SelectCommand.Parameters.Clear();
return ds; } }
然后指定数据控件的数据源 为 以上那个 dataset 就行了。 this.Repeater1.DataSource = ds.Tables[0].DefaultView;
this.Repeater1.DataBind();-----------------------------------但是现在我遇到了一个这样的问题,就是 我无法为以上那个方法提供 cmdText ,就是说,我没有固定的 sql 语句,如:
string sql = "select * from 要分页的数据表"
而是,因为我的数据的特殊性,我的最终要分页的数据源,是一个经过我整理后,重新构建的一个 DataTable ,我要用 aspnetpager 如何对这个 DataTable 进行分页呢?
方法是这样的:
public static DataSet ExecuteWebDiyerDataset(string connString, CommandType cmdType, string cmdText, int mypageindex, int mypagesize, params SqlParameter[] cmdParms)
{
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.SelectCommand = new SqlCommand(); using (SqlConnection conn = new SqlConnection(connString))
{
PrepareCommand(myAdapter.SelectCommand, conn, null, cmdType, cmdText, cmdParms);
DataSet ds = new DataSet();
myAdapter.Fill(ds, mypagesize * (mypageindex - 1), mypagesize, "dtable");
myAdapter.SelectCommand.Parameters.Clear();
return ds; } }
然后指定数据控件的数据源 为 以上那个 dataset 就行了。 this.Repeater1.DataSource = ds.Tables[0].DefaultView;
this.Repeater1.DataBind();-----------------------------------但是现在我遇到了一个这样的问题,就是 我无法为以上那个方法提供 cmdText ,就是说,我没有固定的 sql 语句,如:
string sql = "select * from 要分页的数据表"
而是,因为我的数据的特殊性,我的最终要分页的数据源,是一个经过我整理后,重新构建的一个 DataTable ,我要用 aspnetpager 如何对这个 DataTable 进行分页呢?
解决方案 »
- 关于JS首页图片轮播,后台可控制替换照片的问题!
- PageIndexChanging 事件怎样触发?
- jquery+webservice实现无刷新回复留言,功能已经实现,但只有每页的第一行才能成功回复留言
- 将sql server 2000中表结构导入成excel表内,非常急,非常急,非常急,非常急!!!
- 帮忙看一下这个存储过程
- 请教大虾:有没有办法实现web页面上的手绘图,并提交到服务端
- jqgrid 如何翻页的时候记录上一页选中的值
- asp.net的问题(希望有经验的解答,分不够再加)
- 求救:iframe 的innerHTML的问题
- 那里有介绍Web Services方面的文章!
- 求助:UpdatePanel中Autopostback不起作用!附源码
- asp.net中怎样设置BUTTON控件的背景图片?
this.Repeater1.DataBind();
AspNetPager 使用实例
2008/07/28 11:32 P.M.
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 news_list : System.Web.UI.Page
{
Db database = new Db(); DataSet Ds = new DataSet(); PagedDataSource pds = new PagedDataSource(); protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["id"] !=null)
{
SqlConnection Conn_del = new SqlConnection(database.strConn); Conn_del.Open(); SqlCommand Cmd_del = new SqlCommand("delete News from News where id ="+Request.QueryString["id"], Conn_del); Cmd_del.ExecuteNonQuery(); Response.Write("<script language=\"javascript\">alert(\"删除成功!\");window.location.href=\"news_list.aspx?classid=" + Session["Classid"] + "\"</script>"); Conn_del.Close();
} if (Request.QueryString["classid"] != null) {
Session.Timeout = 60; Session["Classid"] = Request.QueryString["classid"]; SqlConnection Conn_class = new SqlConnection(database.strConn); Conn_class.Open(); SqlCommand com_class = new SqlCommand("select Class from Class where id =" + Request.QueryString["classid"], Conn_class); SqlDataReader dr_class = com_class.ExecuteReader(); if (dr_class.Read())
{
Label1.Text = dr_class["Class"].ToString() + "信息管理";
}
Conn_class.Close();
//列表分页
SqlConnection Conn = new SqlConnection(database.strConn); SqlDataAdapter Da = new SqlDataAdapter("select * from News where classid=" + Request.QueryString["classid"] + "order by id desc", Conn); Da.Fill(Ds, "News"); pds.DataSource = Ds.Tables["News"].DefaultView; AspNetPager1.RecordCount = pds.Count;//在分页之前将记录总数赋值给AspNetPager的记录统计属性 pds.AllowPaging = true; pds.PageSize = 8;//每页的记录数 AspNetPager1.PageSize = pds.PageSize;//将PagedDataSource的页数赋值给AspNetPager的PageSize属性 if (!IsPostBack)
{
Repeater1.DataSource = pds; Repeater1.DataBind();
} Conn.Close();
} } protected void AspNetPager1_PageChanged(object sender, EventArgs e)
{
BindGrid();
} public void BindGrid()
{
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;//索引同步
Repeater1.DataSource = pds;//数据绑定
Repeater1.DataBind();
}
}
/// <summary>
/// 自定义分页方法AspNetPager1
/// </summary>
public void datalistBind()
{
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open(); SqlDataAdapter da = new SqlDataAdapter( "select * from Ware ", conn);
DataSet ds = new DataSet();
da.Fill(ds, AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "Ware ");
AspNetPager1.AlwaysShow = true; //总是现实分页
AspNetPager1.PageSize = 3; //每页显示的数量 DataList1.DataSource = ds;
DataList1.DataBind();
} protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BinDataList4();
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open(); SqlCommand cmd = new SqlCommand( "select count(*) from Ware ", conn);
cmd.Connection = conn;
AspNetPager1.RecordCount = (int)cmd.ExecuteScalar(); //获取表中信息的数量
AspNetPager2.RecordCount = (int)cmd.ExecuteScalar();
conn.Close(); datalistBind();
DataList();
}
} /// <summary>
/// AspNetPager1分页
/// </summary>
/// PageChanging事件
/// <param name= "src "> </param>
/// <param name= "e "> </param>
protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
datalistBind();
}
http://www.cnblogs.com/pochonlee/archive/2008/05/17/1201374.html