首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

Aspnetpager repeater翻页有关问题

2013-06-26 
Aspnetpager repeater翻页问题protected void Page_Load(object sender, EventArgs e){if (!Page.IsPostBa

Aspnetpager repeater翻页问题
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

           // userinfo();
            using (OleDbConnection connection = new OleDbConnection(SQLHelper.connectionString))
            {
                string sql = "select count(id) as num from [content]";
                if (connection.State != ConnectionState.Open)
                    connection.Open();
                OleDbCommand cmd = new OleDbCommand(sql, connection);
                object val = new object();
                try
                {
                    aspnetpager.RecordCount =Convert.ToInt32( cmd.ExecuteScalar());
                    
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                connection.Close();
                Bind();
            }

        }
    }
  private void Bind()
    {
            ds.Clear();
               OleDbConnection connection = new OleDbConnection(SQLHelper.connectionString);
        
            string sqlStr = "select co.[id],co.[title],co.[content],co.publisher,co.readcount,co.simplepicid,co.[time],si.picaddress,si.type from [content] co,simplepic si where co.simplepicid=si.id order by co.id desc";
            if (connection.State != ConnectionState.Open)
                connection.Open();
            OleDbCommand cmd = new OleDbCommand(sqlStr, connection);


            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
           

                
                try
                {

                    da.Fill(ds, aspnetpager.PageSize*(aspnetpager.CurrentPageIndex-1), aspnetpager.PageSize, "dss");

                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                rptmain.DataSource =ds.Tables["dss"];              
                rptmain.DataBind();
                int a = ds.Tables["dss"].Rows.Count;
                connection.Close();
            
        
    }
一共八条数据,我设置页大小为5条,首次加载第一页显示5条数据,按下一页就变成了六条数据,然后又按第一页变成了8条数据,求解啊,是什么问题啊? aspnetpager repeater 翻页
[解决办法]
你这样肯定是会出错的,一般aspnetpager.RecordCount 是绑定的同时,一起附值,不能分开,因为考虑有人在浏览的同时,其它用户更新了数据库
[解决办法]
string?sqlStr?=?"select?co.[id],co.[title],co.[content],co.publisher,co.readcount,co.simplepicid,co.[time],si.picaddress,si.type?from?[content]?co,simplepic?si?where?co.simplepicid=si.id?order?by?co.id?desc";
你这个数据都没有分页,你的控件怎么分页?
你要么使用 分页读取方法,要么使用分页存储过程。

另外你的代码如果不会分层,最起码你要学会把数据交互等方法写到一个类里。
不要在一个页面的CS里写一堆数据库连接和读取。
[解决办法]
分页事件呢?

/// <summary>
        /// 控制页面分页
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        public void ChangePage(object src, PageChangedEventArgs e)


        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            Bind();
        }


前台的分页控件要有OnPageChanged="ChangePage"属性

热点排行