日期:2014-05-17  浏览次数:20504 次

大师快来,小弟需要求助
我写了个类,有个方法 public PagedDataSource getware()
  {
  string strcon = ConfigurationManager.ConnectionStrings["SuperMarketDBConnectionString"].ConnectionString;
  string str = "select * from T_Ware";
  SqlConnection con=new SqlConnection ();
  con.ConnectionString =strcon ;
  SqlDataAdapter sda = new SqlDataAdapter(str, con);
  DataSet ds = new DataSet();
  sda.Fill(ds, "Ware");
  PagedDataSource pds= new PagedDataSource();
  pds.DataSource = ds.Tables["Ware"].DefaultView;
  pds.AllowPaging = true;
  pds.PageSize = 6;
  return pds;
   
  }
内容页里面有个datalist和objectdatasourse,我用objectdatasourse绑定到这个方法,在datalist绑定objectdatasourse,可是我数据库里面有14条记录,我要分3页来显示,然后我在内容页立面图添加了2个linkbutton,可是我就是起不到,上一页,下一页的效果

pageload里面的代码是

  protected void Page_Load(object sender, EventArgs e)
  {
  if (!IsPostBack)
  {
  Class1 cs = new Class1();
  PagedDataSource pds = cs.getware();
  Labeltotal.Text = "共" + pds.PageCount + "页";
  Labelcurrent.Text = "当前第" + ((pds.CurrentPageIndex) + 1).ToString() + "页";
  Session["pds"] = pds;
  }

  }
  protected void LinkButton3_Click(object sender, EventArgs e)
  {
  PagedDataSource pds = (PagedDataSource)Session["pds"];
  if (!pds.IsFirstPage)
  pds.CurrentPageIndex -= 1;
  }
  protected void LinkButton4_Click(object sender, EventArgs e)
  {
  PagedDataSource pds = (PagedDataSource)Session["pds"];
  if (!pds.IsLastPage )
  pds.CurrentPageIndex += 1;
  }

我无论用viewstate还是session都没效果

------解决方案--------------------
首先我暂时不知道你这是什么原因,会不会是数据太多了?这种方法没用过,后期该的话害死你!!!
我可以推荐个方法给你
注意:我这个暂时写的是Oracle的分页句子,真正的分页
C# code

        //分页方法
        /// <summary>
        /// 分页获取数据
        /// </summary>
        /// <param name="tableName">数据表名称</param>
        /// <param name="columns">列名,为空则默认检索所有字段</param>
        /// <param name="where">检索条件,可以为空</param>
        /// <param name="orderColumns">排序字段+排序规则</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="pageIndex">要显示的页码</param>
        /// <param name="recordCount">总记录数</param>
        /// <returns></returns>
        public DataSet GetData(string tableName, string columns, string where, string orderColumns, int pageSize, int pageIndex, ref int recordCount)
        {
            if (string.IsNullOrEmpty(tableName))
            {
                return null;
            }
            else
            {
                string getCountSql = string.Format("SELECT COUNT (*)  FROM {0} WHERE 1=1 {1}", tableName, where);
                recordCount = tools.GetRecordCount(getCountSql);
                int rowStart = pageIndex * pageSize;
                int rowEnd = (pageIndex + 1) * pageSize;
                string sql = "";
                if (!string.IsNullOrEmpty(orderColumns))
                {
                    orderColumns = " order by " + orderColumns;
                }
                if (string.IsNullOrEmpty(columns))
                { 
                    columns = " * ";