当前位置: 代码迷 >> ASP.NET >> 留言板的有关问题 请各位提示一下
  详细解决方案

留言板的有关问题 请各位提示一下

热度:4283   发布时间:2013-02-25 00:00:00.0
留言板的问题 请各位提示一下
在编写留言板时 ASP.Net,从数据库读出 的留言,显示在网页上 ,怎么设置每10条分一个页面,帮忙提醒一下 谢了

------解决方案--------------------------------------------------------
用GridView分页:
只需设置一下:
AllowPaging=true;
和PageSize=数字;
用PagedDataSource分页后台代码是这样的:

C# code
articles= sm.GetArticles();//articles是List<Articles>类型        pds.DataSource = articles;        this.GridView1.DataSource = pds;//pds为PagedDataSource类型的对象        pds.PageSize = 3;        pds.AllowPaging = true;        if (Request.QueryString["page"] == null)        {            currentPage = 0;        }        else        {            currentPage = int.Parse(Request.QueryString["page"]);        }        pds.CurrentPageIndex = currentPage;        if (!pds.IsFirstPage)        {            this.HyperLink1.NavigateUrl = "GridView.aspx?page=" + (currentPage - 1).ToString();        }        if (!pds.IsLastPage)        {            this.HyperLink2.NavigateUrl = "GridView.aspx?page=" + (currentPage + 1).ToString();        }
------解决方案--------------------------------------------------------
如果采用GridView进行分页
首先先将 AllowPaging属性设置为true
然后将PageSize设置为10
最后在分页事件(PageIndexChanging)中写入:GridView.PageIndex = e.NewPageIndex.
即可。
前提是DataSource必须是已经绑定到GridView中

使用DataList获这Repeater分页
可以使用PagedDataSource去绑定一个数据集
PagedDataSource pd = new PagedDataSource();
pd.PageSize = 10;
pd.AllowPaging = true;
pd.CurrentPageIndex = 0;
pd.DataSource = (List<>数据集);
最后设置DataList或者repeater的DataSource=pd;
换页的时候只要改变CurrentPageIndex的值后重新绑定数据就可以
pd.Count可以获得总的分页数
  相关解决方案