绑定Repeater实现分页获取数据

来源:互联网 发布:网络歌手灰色天空 编辑:程序博客网 时间:2024/05/17 18:45
<pre name="code" class="csharp">public partial class _Default : System.Web.UI.Page {    int currentpage = 0;    int indexSize = 2;    protected void Page_Load(object sender, EventArgs e)    {               if (!IsPostBack)        {            List<string> values = new List<string>();            values.Add("Apple");            values.Add("Orange");            values.Add("Pear");            values.Add("Banana");            values.Add("Grape");            rpt.DataSource = values;            rpt.DataBind();            Session.Add("pageSize", indexSize);            Session.Add("source", values);                  }    }    private List<string> get(int index)    {        List<string> data = (List<string>)Session["source"];        List<string> result = new List<string>();        int pageSize= (int)Session["pageSize"];        int bigIndex = (int)System.Math.Ceiling(Convert.ToDecimal(data.Count) / indexSize);        if (bigIndex<index)        {            return new List<string>();        }        int first = index == 1 ? 0 : (index - 1) * indexSize;        int end = bigIndex == index ? data.Count : index* pageSize;        for (int i = first; i < end; i++)        {            result.Add(data[i]);        }        return result;    }    protected void pageindex_TextChanged(object sender, EventArgs e)    {        bool success = Int32.TryParse(pageindex.Text, out currentpage );        if (success)        {            List<string> result = get(currentpage);            rpt.DataSource = result;            rpt.DataBind();        }           }}


                                             
0 0