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

小白不会使用AspNetPager,请高人指教!
C# code

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int totalOrders = (int)SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "P_GetOrderNumber");
            AspNetPager1.RecordCount = totalOrders;
            //bindData(); //使用url分页,只需在分页事件处理程序中绑定数据即可,无需在Page_Load中绑定,否则会导致数据被绑定两次
        }
    }

    void bindData()
    {
        Repeater1.DataSource = SqlHelper.ExecuteReader(CommandType.StoredProcedure, ConfigurationManager.AppSettings["pagedSPName"],
            new SqlParameter("@startIndex", AspNetPager1.StartRecordIndex),
            new SqlParameter("@endIndex", AspNetPager1.EndRecordIndex));
        Repeater1.DataBind();
    }

    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        bindData();
    }



这是官方给的Repeater的事例,我基本能看懂。问题是我太菜了他用了存储过程,我根本不懂存储过程。

第一 Page_Load的时候我没理解错应该是先取出记录总数,这个就不会了。
第二 绑定数据的时候他用的存储过程,我想改成不用存储过程传统的绑定就不会了。

------解决方案--------------------
C# code

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int pageIndex = 1;
            int.TryParse(Request.QueryString["Page"], out pageIndex);
            int _counts=0;
            
            rptList.DataSource = CutPageData(pageIndex, AspNetPager1.PageSize, "outInfos", "id", _where, "id desc", out _counts);
            rptList.DataBind();
            AspNetPager1.RecordCount = _counts;
        }
    }
    /// <summary>
    /// 分頁SQL語句 
    /// </summary>
    /// <param name="_pageIndex">當前頁</param>
    /// <param name="_pageSize">每頁大小</param>
    /// <param name="_table">表</param>
    /// <param name="_id">主鍵</param>
    /// <param name="_strWhere">條件</param>
    /// <param name="_strOrder">排序</param>
    /// <param name="_counts">總記錄數</param>
    /// <returns></returns>
    public DataSet CutPageData(int _pageIndex, int _pageSize, string _table, string _id, string _strWhere, string _strOrder, out int _counts)
    {
        System.Text.StringBuilder strSql = new System.Text.StringBuilder();
        _pageIndex = _pageIndex < 1 ? 1 : _pageIndex;
        if (_pageIndex == 1)
        {
            strSql.Append("select top " + _pageSize + " * from");
            strSql.Append(" " + _table);
            if (!string.IsNullOrEmpty(_strWhere))
            {
                strSql.Append(" where 1=1 and " + _strWhere);
            }
            strSql.Append(" order by " + _strOrder);
        }
        else
        {
            strSql.Append("select top " + _pageSize + " * from");
            strSql.Append(" " + _table);
            strSql.Append(" where " + _id + " not in (select top " + _pageSize * (_pageIndex - 1) + " " + _id + " from " + _table + (string.IsNullOrEmpty(_strWhere) ? _strWhere : " where " + _strWhere) + " order by " + _strOrder + ")");
            if (!string.IsNullOrEmpty(_strWhere))
            {
                strSql.Append(" and " + _strWhere);
            }
            strSql.Append(" order by " + _strOrder);
        }
        string strCount = "select count(*) from " + _table + (string.IsNullOrEmpty(_strWhere) ? _strWhere : " where " + _strWhere);
        SqlConnection conn = new SqlConnection("鏈接");
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(s