.net静态页 输出新闻列表,带分页

作者:互联网   出处:控件中国网   2014-11-05 19:05:27   阅读:1

.net静态页 输出新闻列表,带分页

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.IO;
namespace helper
{
    public class helper
    {
        public static DataSet ExecuteDataset(string connString, CommandType txtType, string sql)
        {
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = txtType;
            cmd.CommandText = sql;
            cmd.Connection = conn;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }
    }
    public class SqlHelper
    {
        /// <summary>
        /// 根据模板读取数据库内容,无需创建其他列表,直接创建html
        /// </summary>
        /// <param name="strFileName">生成的网页文件名</param>
        /// <param name="strTmplPath">网页模板文件的路径</param>
        /// <param name="str">替换后的html文件</param>
        public static string re_lable(string strTmplPath)
        {
            //取模板文件的内容
            System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");
            StreamReader sr = null;
            StreamWriter sw = null;
            string str = "";
            try
            {
                sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("./") + "HTMLPage.htm", code);
                str = sr.ReadToEnd(); // 读取文件
                sr.Close();
            }
            catch (Exception exp)
            {
                Console.Write(exp);
            }
            return str;
        }
        public static string read_sys(string str)
        {
            return str;
        }
    }
    public class SplitPage
    {
        /// <summary>
        /// 根据模板读取数据库内容,无需创建其他列表,直接创建html
        /// </summary>
        /// <param name="strFileName">[$NewsList$]</param>
        /// <param name="strTmplPath">网页模板文件的路径</param>
        /// <param name="str">替换后的html文件</param>
        ///
        public static void splitpage(string htm, int pagesize)
        {
            string connString = "Data

Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True";
            string sql = "select * from ProList";
            DataSet ds = helper.ExecuteDataset(connString, CommandType.Text, sql);//helper类
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count / pagesize >= 1)
            {
                int maxi = 0;
                //取出所有新闻列表
                if (dt.Rows.Count % pagesize == 0)
                {
                    maxi = dt.Rows.Count / pagesize;
                }
                else
                {
                    maxi = (dt.Rows.Count / pagesize) + 1;
                }
                for (int i = 0; i < maxi; i++) // i 分页的页数(生成页面的个数)
                {
                    StringBuilder sb = new StringBuilder();//新闻列表
                    //生成新闻列表
                    for (int h = i * pagesize; h < (i + 1) * pagesize; h++)//
                    {
                        if (h < dt.Rows.Count)
                        {
                            sb.Append("<li><span class=\"newstitle\"><a href=\"/news/" + dt.Rows[h]

["ID"].ToString() + ".html\" title=\"" + dt.Rows[h]["Name"].ToString() + "\">" + dt.Rows[h]["Name"].ToString() +

"</a></span><div class=\"clear\"></div></li>");
                        }
                    }
                    sb.Append("<div class=\"changepage\">"); //maxi
                    sb.Append("<div class=\"pre1\"><a href=\"news_1.html\"><img src=\"pre1.jpg\" width=\"15\"

height=\"10\" border=\"0\" alt=\"返回首页\" /></a></div>");
                    if (i == 0)//判断如果为第一页,则不把“上一页”设为超链接
                    {
                        sb.Append("<div class=\"pre2\"><a href=\"#\"><img src=\"pre2.jpg\" width=\"11\"

height=\"10\" border=\"0\" alt=\"上一页\" /></a></div>");
                    }
                    else
                    {
                        sb.Append("<div class=\"pre2\"><a href=\"news_" + i + ".html\"><img src=\"pre2.jpg\"

width=\"11\" height=\"10\" border=\"0\" alt=\"上一页\" /></a></div>");
                    }
                    if (i < 10)
                    {
                        for (int f = 1; f < 10; f++)//每页显示9个分页数字
                        {
                            if (f == i + 1)
                            {
                                sb.Append(" <div class=\"num\"><a href=\"news_" + f.ToString() + ".html\"

class=\"curpage\">" + f.ToString() + "</a></div>");
                            }
                            else if (f <= maxi)
                            {
                                sb.Append("<div class=\"num\"><a href=\"news_" + f.ToString() + ".html\">" +

f.ToString() + "</a></div>");
                            }
                        }
                    }
                    else
                    {
                        int maxfeye = i + 5;
                        int minfeye = i - 5;
                        for (int f = minfeye; f < maxfeye; f++)//每页显示9个分页数字
                        {
                            if (f == i + 1)
                            {
                                sb.Append("<div class=\"num\"><a href=\"news_" + f.ToString() + ".html\"

class=\"curpage\">" + f.ToString() + "</a></div>");
                            }
                            else if (f <= maxi)
                            {
                                sb.Append("<div class=\"num\"><a href=\"news_" + f.ToString() + ".html\">" +

f.ToString() + "</a></div>");
                            }
                        }
                    }
                    if (i == dt.Rows.Count / pagesize) //判断如果为最后一页,则不把“下一页”设为超链接
                    {
                        sb.Append("<a href=\"#\"><img src=\"next2.jpg\" width=\"11\" height=\"10\" border=\"0\"

alt=\"下一页\" /></a>");
                    }
                    else
                    {
                        sb.Append("<div class=\"pre2\"><a href=\"news_" + (i + 2) + ".html\"><img

src=\"next2.jpg\" width=\"11\" height=\"10\" border=\"0\" alt=\"下一页\" /></a></div>");
                    }
                    sb.Append("<div class=\"pre1\"><a href=\"news_" + (maxi).ToString() + ".html\"><img

src=\"next1.jpg\" width=\"15\" height=\"10\" border=\"0\" alt=\"末页\" /></a></div>");
                    sb.Append("<div class=\"clear\"></div> </div>");

                    Encoding code = Encoding.GetEncoding("gb2312");
                    StreamReader sr = null;
                    StreamWriter sw = null;
                    string str = null;
                    str = SqlHelper.re_lable(htm);
                    //根据新闻的ID
                   
                    int fileName = i + 1;
                    string CreateFileName = "news_" + fileName.ToString();
                    str = str.Replace("[$NewsList$]", sb.ToString());//替换列表标签
                    //生成静态文件
                    try
                    {
                        str = SqlHelper.read_sys(str);
                        sw = new StreamWriter(System.Web.HttpContext.Current.Server.MapPath("./") + CreateFileName

+ ".html", false, code);
                        sw.Write(str);
                        sw.Flush();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        sw.Close();
                    }
                }//for语句结束
               
            }
        }
    }

 

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat