我在 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 进行分页呢?
解决方案 »
- 求学习计算机基础知识的资料!
- crm for outlook offline如何二次开发离线程序?
- 关于导出数据的一个问题
- 问一个关于Linq多层结构的问题!
- 研究讨论下分页控件的问题!大家多多积极参送分活动!在网上看到sqlPager控件资料比较多,这控件有没有人用过....
- 博客的开发
- 好消息,最好的全能空间,有需要的进来,已经稳定运行好多年了
- 急!!还是无法创建或打开本地 IIS 网站
- 有点模糊:设置Response.Expires与outputcache有什么区别呢?
- 网页显示数据的排序问题 极其困难
- 求助: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