首页 > 学院 > 开发设计 > 正文

Datatable paging,Repeater with Paging

2019-11-15 02:31:52
字体:
来源:转载
供稿:网友

Datatable paging,Repeater with Paging

/// <summary>    /// 塗聚文    /// 20140225    /// </summary>    public partial class DatatablePage : System.Web.UI.Page    {        /// <summary>        /// 测试 分页数据        /// </summary>        DataTable pageDt;        /// <summary>        ///         /// </summary>        public void geovinduDt()        {            pageDt = new DataTable();            pageDt.Columns.Add("id", typeof(int));            pageDt.Columns.Add("ddno", typeof(string));            pageDt.Columns.Add("dd", typeof(string));            //dtyhdd.Rows.Add("1", "n1");            //dtyhdd.Rows.Add("2", "n2");            for (int i = 1; i <= 31; i++)            {                //保存到内存表中                DataRow tr = this.pageDt.NewRow();                tr["id"] = i;                tr["ddno"] = i.ToString();                tr["dd"] = i.ToString() + "内容";                pageDt.Rows.Add(tr);            }        }        /// <summary>        /// 隐患地点共分多少页        /// </summary>        int verypage = 0;        /// <summary>        /// 隐患当前页        /// </summary>        int currentPage = 0;        /// <summary>        /// 每页显示多少数据        /// </summary>        int PageSize = 1;        /// <summary>        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        PRotected void Page_Load(object sender, EventArgs e)        {                                    if (!IsPostBack)            {                if (!string.IsNullOrEmpty(Request.QueryString["page"]))                {                    currentPage = int.Parse(Request.QueryString["page"]);                }                currentPage++;                this.TextBox1.Text = currentPage.ToString();                //【初始化数据】                geovinduDt();                //【执行分页显示】                //每次显示10条数据 需要多少次                verypage = pageDt.Rows.Count / 1;                //是否有余数                int numys = pageDt.Rows.Count % 1;                if (numys > 0)                {                    currentPage++;                }                //comyhdd.Items.Clear();                if (currentPage >= pageDt.Rows.Count)                {                    Button1.Enabled = false;                     return;                }                DataRow[] mMatches = pageDt.Select("(id=" + currentPage + ")");                //DataRow matches = new DataRow();                string strName = mMatches[0]["dd"].ToString();                DataSet pDS = new System.Data.DataSet();                System.Data.DataTable mDT = pageDt.Clone();                mDT.Clear();                //mDT.TableName = mTableName;                //mDT = mDT.Clone();                for (int i = 0; i < mMatches.Length; i++)                {                    mDT.Rows.Add(mMatches[i].ItemArray);                }                pDS.Tables.Add(mDT);                //1                Repeater1.DataSource = GetPagedTable(pageDt, currentPage, PageSize);                Repeater1.DataBind();                //2                //Repeater1.DataSource = pDS;                //Repeater1.DataBind();                this.TextBox1.Text = strName;                //Repeater1.DataSource = GetPagedTable(pageDt, yhdddqi, PageSize);                //Repeater1.DataBind();                this.Label1.Text = currentPage.ToString() + "/" + verypage;                            }        }        /// <summary>        ///         /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        protected void Button1_Click(object sender, EventArgs e)        {            verypage = 1;              //下一页            if (!string.IsNullOrEmpty(Request.QueryString["page"]))            {                currentPage = int.Parse(Request.QueryString["page"]);            }            currentPage++;            geovinduDt();            if (currentPage >= pageDt.Rows.Count)            {                Button1.Enabled = false;                 return;            }            DataRow[] mMatches = pageDt.Select("(id=" + currentPage + ")");            //DataRow matches = new DataRow();            string strName = mMatches[0]["dd"].ToString();            this.TextBox1.Text = strName;            string answer = string.Empty;            if (CheckBoxA.Checked == true)            {                answer = "A";            }            if (CheckBoxB.Checked == true)            {                if (!string.IsNullOrEmpty(answer))                {                    answer = answer + "," + "B";                }                else                {                    answer = "B";                }            }            if (CheckBoxC.Checked == true)            {                if (!string.IsNullOrEmpty(answer))                {                    answer = answer + "," + "C";                }                else                {                    answer = "C";                }            }            if (CheckBoxD.Checked == true)            {                if (!string.IsNullOrEmpty(answer))                {                    answer = answer + "," + "D";                }                else                {                    answer = "D";                }            }            Jscript.Alert(answer);            this.Label1.Text = currentPage.ToString() + "/" + verypage;            Response.Redirect("DatatablePage.aspx?page=" + (currentPage) + "&name=" + strName);                        }        /// <summary>        /// DataTable分页        /// </summary>        /// <param name="dt">DataTable</param>        /// <param name="PageIndex">页索引,注意:从1开始</param>        /// <param name="PageSize">每页大小</param>        /// <returns></returns>        public static DataTable GetPagedTable(DataTable dt, int currentPageIndex, int pageSize)        {            //1            //判断当前索引            if (currentPageIndex == 0)                return dt;            //从数据集合拷贝数据            DataTable newdt = dt.Copy();            //数据清空            newdt.Clear();            //开始数据索引 = 当前页-1 x 每页大小            int rowbegin = (currentPageIndex - 1) * pageSize;            //结束数据索引 = 当前页 x 每页大小            int rowend = currentPageIndex * pageSize;            //开始数据索引 大于等于 当前数据集合大小            if (rowbegin >= dt.Rows.Count)                return newdt;            //结束数据索引 大于 当前数据集合大小            if (rowend > dt.Rows.Count)                rowend = dt.Rows.Count;            //遍历数据            for (int i = rowbegin; i <= rowend - 1; i++)            {                DataRow newdr = newdt.NewRow();                DataRow dr = dt.Rows[i];                foreach (DataColumn column in dt.Columns)                {                    newdr[column.ColumnName] = dr[column.ColumnName];                }                newdt.Rows.Add(newdr);            }            return newdt;            //2            //if (currentPageIndex == 0)            //{            //return dt;            //}            //DataTable newdt = dt.Clone();// dt.Copy();                         //int rowbegin = (currentPageIndex - 1) * pageSize;//当前页的第一条数据在dt中的位置            //int rowend = currentPageIndex * pageSize;//当前页的最后一条数据在dt中的位置                         //if (rowbegin >= dt.Rows.Count)            //{            //    return newdt;            //}                         //if (rowend > dt.Rows.Count)            //{            //    rowend = dt.Rows.Count;            //}                         //DataView dv = dt.DefaultView;            //for (int i = rowbegin; i <= rowend - 1; i++)            //{            //    newdt.ImportRow(dv[i].Row);            //}                         //return newdt;                    }    }

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DatatablePage.aspx.cs" Inherits="ShoppingDeals.Admin.VipHKExamSystem.DatatablePage" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表