Beispiel #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            listNewsWithPages ln = new listNewsWithPages();
            DataModal dm = new DataModal();
            if (!(Request.Params["page"] == null || Request.Params["page"] == ""))
            {
                try
                {
                    page = Convert.ToInt32(Request.Params["page"]);
                }
                catch (Exception ex)
                {
                    page = 1;
                }
            }

            int pageSize = 25;
            ln = dm.GetNewsList(page,pageSize);
            this.NewsRepeater.DataSource = ln.newsList;
            this.NewsRepeater.DataBind();
            pages = (ln.Total % pageSize) > 0 ? (ln.Total / pageSize)+1 : (ln.Total / pageSize);
        }
Beispiel #2
0
        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="page">第几页</param>
        /// <param name="pageSize">每页数量</param>
        /// <param name="sort">排序元</param>
        /// <param name="order">desc或asc</param>
        /// <returns></returns>
        public listNewsWithPages GetNewsList(int page, int pageSize, string sort, string order)
        {
            listNewsWithPages lnp = new listNewsWithPages();
            List<News> ns = new List<News>();
            conn.Open();
            if (page == 1)
            {
                cmd.CommandText = "select top " + pageSize +
                         "  newsID,newsTitle,newsPostTime from  newsTB  order by " + sort + " " + order + " ";

            }
            else
            {
                cmd.CommandText = "select top " + pageSize +
                           "  newsID,newsTitle,newsPostTime from  newsTB where  newsID not in ( select top " + (page - 1) * pageSize +
                           " newsID from newsTB   order by " + sort + " " + order + " )  order by " + sort + " " + order + " ";

            }
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                News n = new  News();
                n.newsID =Convert.ToInt32( dr["newsID"]);
                n.newsTitle = dr["newsTitle"].ToString();
                n.newsContent = "";
                n.newsPostTime = (DateTime)dr["newsPostTime"];
                ns.Add(n);
            }
            dr.Close();
            //查询页数
            cmd.CommandText = "select count(newsID) from newsTB";
            int tl = Convert.ToInt32(cmd.ExecuteScalar());
            lnp.Total = tl;
            conn.Close();
            lnp.newsList = ns;
            return lnp;
        }