.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语句结束
                
            }
        }
    }

来自: http://hi.baidu.com/wuhen639/blog/item/9c8c1c95f78a1b41d0135ef7.html

转载于:https://www.cnblogs.com/fuyu-blog/archive/2011/08/16/2140548.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值