当前位置:编程学习 > C#/ASP.NET >>

ASP.NETPagedDataSource 实现分页的问题?


public partial class _232DataList : System.Web.UI.Page
    {
        PagedDataSource pds = new PagedDataSource();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindDL(0);

            }
        }
        public void bindDL(int curPage)
        {
           
            pds.AllowPaging = true;
            pds.PageSize = 2;
            pds.CurrentPageIndex = curPage;

            //绑定数据源
            ShowImageBll ShowImageBll = new BLL.ShowImageBll();
            DataSet ds=ShowImageBll.GetList();
            pds.DataSource = ds.Tables[0].DefaultView;
            dlPhoto.DataSource = pds;
            dlPhoto.DataBind();

        }

        protected void dlPhoto_ItemCommand(object source, DataListCommandEventArgs e)
        {
           
            switch (e.CommandName)
            {
                case "first":
                    if (pds.CurrentPageIndex != 0)
                    {
                        pds.CurrentPageIndex = 0;
                    }
                    else
                    {
                        Response.Write("<script language=javascript>" + "alert(\"已经是第一页\")" + "</script>");
                    }
                    bindDL(pds.CurrentPageIndex);
                    break;
                case "pre":
                    if (pds.CurrentPageIndex > 0) {
                        pds.CurrentPageIndex = pds.CurrentPageIndex - 1;
                        bindDL(pds.CurrentPageIndex);
                    }
                    else
                    {
                        Response.Write("<script language=javascript>" + "alert(\"已经是第一页\")" + "</script>");
                    }
                    
                    break;
                case "next":
                    if (pds.CurrentPageIndex < pds.PageCount - 1)
                    {
                        pds.CurrentPageIndex = pds.CurrentPageIndex + 1;
                        bindDL(pds.CurrentPageIndex);
                    }
                    else
                    {
                        Response.Write("<script language=javascript>" + "alert(\"已经是最后一页\")" + "</script>");
                    }
                   
                    break;
                case "last":
                    if (pds.CurrentPageIndex != pds.PageCount - 1)
                    {
                        pds.CurrentPageIndex = pds.PageCount - 1;

                    }
                    else
                    {
                        Response.Write("<script language=javascript>" + "alert(\"已经是最后一页\")" + "</script>");
                    }
                    bindDL(pds.CurrentPageIndex);
                    break;
                case "search":
                    if (e.Item.ItemType == ListItemType.Footer)
                    {
                        int pageCount = int.Parse(pds.PageCount.ToString ());
                        TextBox txtPage = e.Item.FindControl("txtPage") as TextBox;
                        int myPage = 0;
                        if(txtPage .Text !=null)
                        {
                          myPage = int.Parse(txtPage .Text.Trim ().ToString ());
                        
                        }
                        if (myPage <=0||myPage >pageCount ) {
                            Response .Write ("<script>alert('请输入正确的页面数!')</script>");
                        
                        }
                        bindDL(myPage-1); 
                    
                    
                    }
                    break;  

}}}


问题:总是显示第一页,点击下一页弹出意识最后一页,点击末页报错。
知道是PagedDataSource pds = new PagedDataSource();的问题 每次都NEW了一个,所以pds.PageCount总是为0,但是不知道给怎么修改?求教  --------------------编程问答-------------------- 方法一、
1、每次提交的时候获取当前页(如传递个page值,比如A.ASPX?page=2,默认为1)
2、后台接收值后(没有值默认为1,即第1页),根据条件后台计算出记录数,总共多少页(需要有每页多少条记录的定义),根据当前页返回记录集。你那好像是每次都返回全部的记录集了。
方法二、
1、可以去网上找分页的存储过程,或者AJAX实现无刷新分页的办法
2、可以用webdiyer大哥开发的 AspNetPager 免费分页控件 ,很好用,很省心(http://bbs.csdn.net/topics/390524552)。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,