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

AspNetPager分页控件的问题
本菜使用AspNetPager分页控件进行分页,设置每页显示1条,表中共有4条记录,但只能看到第一页的,没法看其他页的,请帮忙解决
 前台代码:
C# code

<body>
    <form id="form1" runat="server">
    
    <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
    <div><%#Eval("name")%></div>
    </ItemTemplate>
        
    </asp:Repeater>
    <webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="true"  
            PageSize="1" CssClass="paginator"   CurrentPageButtonClass="cpb"
         LastPageText="尾页" FirstPageText="首页" PrevPageText="上一页" NextPageText="下一页"
            UrlPaging="True" NumericButtonTextFormatString="{0}" 
            ShowCustomInfoSection="Right" 
        onpagechanged="AspNetPager1_PageChanged"  CustomInfoTextAlign="Left" 
        LayoutType="Div" >
        </webdiyer:AspNetPager>

    </form>
</body>





后台代码:
C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Wuqi.Webdiyer;

namespace c_displays
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            Bind();
        }

        public void Bind()
        {
            this.AspNetPager1.RecordCount = linksql.count("select * from good");
            SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["AppServices"]);
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from good order by id desc", conn);
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            sda.Fill(ds, AspNetPager1.PageSize * (this.AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "temp");
            DataTable dt = ds.Tables["temp"];
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            conn.Close();  
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            Bind();
        }
        
    }
}





------解决方案--------------------
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["AppServices"]);
conn.Open();
SqlCommand cmd = new SqlCommand("select * from good order by id desc", conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
AspNetPager1.RecordCount = ds.Tables[0].Rows.Count;
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
pds.PageSize = AspNetPager1.PageSize;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;

rtInfo.DataSource = pds;
rtInfo.DataBind();
这样写