导航

DataList分页与GridView传统分页

Posted on 2006-10-24 10:48  madduck  阅读(186)  评论(0编辑  收藏  举报

参考《Asp.net网络开发技术》和网上某Datalist的分页技术(找不到网址了)
SqlConnection consearch = new SqlConnection(ConfigurationManager.AppSettings["connstr"]);
                    SqlDataAdapter da = new SqlDataAdapter("SearchPro", consearch);
                    da.SelectCommand.CommandType = CommandType.StoredProcedure;

                    SqlParameter paramCatID = new SqlParameter("@CatID", SqlDbType.Int);
                    paramCatID.Value = CatID;
                    da.SelectCommand.Parameters.Add(paramCatID);
                    SqlParameter paramProName = new SqlParameter("@ProName", SqlDbType.NVarChar, 50);
                    paramProName.Value = ProName;
                    da.SelectCommand.Parameters.Add(paramProName);

                    consearch.Open();
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    consearch.Close();

                    //设置分页
                    PagedDataSource pgsearch = new PagedDataSource();
                    pgsearch.DataSource = ds.Tables[0].DefaultView;
                    pgsearch.AllowPaging = true;
                    pgsearch.PageSize = 6;

                    int CurPage;
                    if (Request.QueryString["Page"] != null)
                    {
                        CurPage = Convert.ToInt32(Request.QueryString["Page"]);
                    }
                    else
                    {
                        CurPage = 1;
                    }
                    pgsearch.CurrentPageIndex = CurPage - 1;
                    lblCurPage.Text = "Page: " + CurPage.ToString();

                    if (!pgsearch.IsFirstPage)
                    {
                        lnkPrev.HRef = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
                    }

                    if (!pgsearch.IsLastPage)
                    {
                        lnkNext.HRef = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
                    }

                    //把PagedDataSource 对象赋给dlsearch
                    dlsearch.DataSource = pgsearch;
                    dlsearch.DataBind();


===================================================================================
下面是GridView的传统分页方法,对比一下
    void BindGrid()
    {
             ShopOnline.shopbase cart = new ShopOnline.shopbase();
            string cartID = cart.GetShoppingCartID();
            if (cart.CountShoppingCartItem(cartID) == 0)
            {
                lbMessage.Text = "你还没有购物!";
            }
            else
            {
                this.gvGoodsCart.DataSource = cart.DisplayShoppingCart(cartID);
                this.gvGoodsCart.DataBind();
                lbTotal.Text = String.Format("{0:c}", cart.ShoppingCartTotalCost(cartID));
            }
    }

    protected void gvGoodsCart_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvGoodsCart.PageIndex = e.NewPageIndex;
        BindGrid();
    }