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

小白不会运用AspNetPager,请高人指教

2012-08-02 
小白不会使用AspNetPager,请高人指教!C# codeprotected void Page_Load(object sender, EventArgs e){if (

小白不会使用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(strSql.ToString(),conn);        da.Fill(ds);        conn.Open();        SqlCommand cmd = new SqlCommand(strCount, conn);        _counts = (int)cmd.ExecuteScalar();        conn.Close();        return ds;    } 


[解决办法]
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AspNetPager1.RecordCount = tb.count();//總數
bind();

}
}
void bind()
{
PagedDataSource pds = new PagedDataSource();
pds.AllowPaging = true;
pds.PageSize = AspNetPager1.PageSize; //PagedDataSource顯示多少條=分頁控件顯示條數
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
pds.DataSource = tb.t_bbssAll(null).DefaultView; //將查詢的數據放給PagedDataSource在將值給 Repeater;
rpt_list.DataSource = pds;
rpt_list.DataBind();
}
[解决办法]
不会呀, 这种是把数据 分成 好几段 来select 并展示 ,所以 获取数据的速度还是很不错的

热点排行