释蝉

博客园 首页 新随笔 联系 订阅 管理

后台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=IOT1706B;Integrated Security=True"); 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BandData();
            }

        }

        private void BandData(int pageIndex=1,int pageSize=6)
        {
            DataTable dt = new DataTable();
            if (Session["dt"]!=null)
            {
                dt = Session["dt"] as DataTable;
            }
            else
            {
                SqlDataAdapter dataAdapter = new SqlDataAdapter("select Id,TiGan,Rsesult from Topic order by Id desc", conn);
                dataAdapter.Fill(dt);
                Session["dt"] = dt;
            }
           
            Session["pageIndex"] = pageIndex;
            Session["totalPage"] = dt.Rows.Count / pageSize + (dt.Rows.Count % pageSize > 0 ? 1 : 0);
            var linq = (from s in dt.AsEnumerable()
                        select new
                        {
                            Id = s.Field<int>("Id"),
                            TiGan = s.Field<string>("TiGan"),
                            Rsesult = s.Field<string>("Rsesult"),
                        }).Skip((pageIndex-1)*pageSize).Take(pageSize);
            GridView1.DataSource = linq.ToList();
            GridView1.DataBind();
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            BandData();
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            int n = (int)Session["pageIndex"] - 1;
            if (n<=0)
            {
                n = 1;
            }
            BandData(n);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            int n = (int)Session["pageIndex"] + 1;
            if (n>= (int)Session["totalPage"])
            {
               n = (int)Session["totalPage"];
            }
            BandData(n);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            int n = (int)Session["totalPage"];
            BandData(n);
        }
    }
}
posted on 2020-01-07 10:05  释蝉  阅读(107)  评论(0编辑  收藏  举报