asp.net 生成静态分页html

前台就几个输入框,内容输入框用的是FreeTextBox框:

 

<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false" CodeBehind="InsHtmlPage.aspx.cs" Inherits="JYWebBook.member.InsHtmlPage" %>

<%@ Register Assembly="FreeTextBox" Namespace="FreeTextBoxControls" TagPrefix="FTB" %>



    添加文章并生成静态HTML页面
   



   


   

       
           
               
               
           
           
               
               
                      
           
               
               
           
           
               
               
           
           
               
               
           
           
               
               
           
       
文章标题:
                   

                    文章来自:

                   

                    文章作者:

                   
文章类别:
                          
                    当前时间:
                          
                   
               

                    文章内容:

                   
               

                   
                   
               

   

   



============================================ .cs文件

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;//新加命名空间
using System.Text;
using System.IO;

namespace JYWebBook.member
{
    public partial class InsHtmlPage : System.Web.UI.Page
    {
        public string url;
        public string strEncodeHtml;
        public string strDecodeHtml;
        protected void Page_Load(object sender, EventArgs e)
        {         
        }
        ///


        /// 添加文章
        ///

        ///
        ///
        protected void btnAdd_ServerClick(object sender, EventArgs e)
        {
            #region 获取页面上的数据
            string strTitle = Request.Form["txtTitle"].ToString().Trim();//接收传过来的标题
            string strfrom = Request.Form["txtUrl"].ToString().Trim(); //文章来自
            string strAuthor = Request.Form["txtAuthor"].ToString().Trim();
            string strType = Request.Form["selType"].ToString().Trim(); //文章类别
            string strAddDate = Request.Form["txtTime"].ToString().Trim();//添加时间

            //此处为解析和过滤文章内容的方法(根据需要写)
            string strContent = Server.HtmlEncode(ftbContent.Text.Trim());//编码:


编码后是 <hr>
            strContent = Server.HtmlDecode(strContent);//解码: <hr> 解码后是:

            #endregion
           
            //添加文章数据到数据库中的方法==========================
            //int intAddResult=remember.addContent();//可在此添加方法中返回一个值,添加成功就生成下面的静态html,否则不生成
            //添加文章数据到数据库中的方法==========================           
            //if (intAddResult = 1)
            //{
                #region 将文章内容按指定长度拆分并保存到数组中(用于静态html分页)
                int intPageSize = 1200;//设置文章内容分页字符串长度(自定义)
                int intTotalPage = 0;
                int intLength = strContent.Length;//文章长度
                string[] content = null;
                if (intPageSize < intLength)
                {//如果分页字符串长度大于文章长度时就不用分页了
                    if (intLength % intPageSize != 0)
                    {
                        intTotalPage = intLength / intPageSize + 1;
                        for (int h = 1; h <= intTotalPage; h++)
                        {
                            if (intTotalPage != 1)
                            {
                                int num2 = intPageSize * h;
                                if (num2 < intLength)
                                {
                                    if (h >= 2)//当循环执行2次或2次以上时,字符串长度要多加(h-1)
                                        strContent = strContent.Insert(num2 + (h - 1), "Θ");
                                    else
                                        strContent = strContent.Insert(num2, "Θ");
                                }
                            }
                        }
                    }
                }
                content = strContent.Split(new Char[] { 'Θ' });//对文章内容进行拆分,并保存到数组中           
                #endregion

                #region 文章内容生成静态html并在指定文件夹中生成
                Random randobj = new Random();//随机数 
                string strDateTime = DateTime.Now.ToString("yyyyMMdd") + "-" + DateTime.Now.ToString("HHmmss");
                string strDate = strDateTime.Trim() + "/" + DateTime.Now.ToString("HHmmss")
                    + randobj.Next(1000, 10000).ToString(); //网页名
                string strFileName = strDate + ".html";     //网页相对地址
                this.url = strFileName;
               
                ///创建当前日期的文件夹开始
                string dir = Server.MapPath("test/" + strDateTime.Trim());//以当前时间为文件夹名
                if (!Directory.Exists(dir))//用来创建文件夹,如果不存在就创建文件夹
                {
                    Directory.CreateDirectory(dir);
                }
                ///创建当前日期的文件夹结束
                try
                {
                    for (int i = 0; i < content.Length; i++)//content为对接收后的文章内容进行拆分,并保存后的数组
                    {
                        StringBuilder strhtml = new StringBuilder();  //需引用 using System.Text,当在一个循环中将许多字符串连接在一起时,使用 StringBuilder 类可以提升性能

                        //创建StreamReader对象读取不同路径下的模板并转换成字符串
                        //switch (strType) //对文章类别进行比较,以判断模板位置,此处只是例子,没做判断
                        //{
                        //    case "文化>>艺术>>流派": //如“文化>>艺术>>流派” 类别下的模板  
                        //        using (StreamReader sr = new StreamReader(Server.MapPath("../art/Template.html"), Encoding.GetEncoding("gb2312")))
                        //        {//StreamReader文本流读取器
                        //            String oneline;
                        //            //读取指定的HTML文件模板
                        //            while ((oneline = sr.ReadLine()) != null) //读取../ad/Template.html"模板"流中的字符串
                        //            {
                        //                strhtml.Append(oneline); //用 StringBuilder 连接字符串
                        //            }
                        //            sr.Close(); //关闭StreamReader文本流读取器   
                        //        }
                        //        break;
                        //    default:  //文章大类不同,读取不同路径下的模板
                        using (StreamReader sr = new StreamReader(Server.MapPath("test/Template.html"), Encoding.GetEncoding("gb2312")))
                        {
                            String oneline;
                            //读取指定的HTML文件模板
                            while ((oneline = sr.ReadLine()) != null)
                            {
                                strhtml.Append(oneline); //拼接从模板html解析出的字符串
                            }
                            sr.Close();//关闭StreamReader文本流读取器   
                        }
                        //        break;
                        //}                   

                        string FilePath = "";
                        strhtml = strhtml.Replace("Title", strTitle);  //替换模板html中的Title为数据库(文章标题)
                        strhtml = strhtml.Replace("strType", strType.Trim());
                        strhtml = strhtml.Replace("strAuthor", strAuthor.Trim());
                        strhtml = strhtml.Replace("strFrom", strfrom.Trim());
                        strhtml = strhtml.Replace("strTime", strAddDate.Trim());
                        strhtml = strhtml.Replace("strContent", content[i]);
                        //strhtml = strhtml.Replace("ArticleUrl", this.url);  //将当前文章的路径及文件名传给评论页以便返回

                        ///对文章内容进行分页
                        int upbound = content.Length;//数组的上限,即长度

                        #region 分页效果:分页在下
                        //string strNumber = "";//数字分页1,2,3……
                        //for (int m = 1; m <= upbound; m++) // upbound 文章内容content数组的上限,即长度
                        //{
                        //    if (m == 1)//如果是第一页就显示成这个样子:20070524.html而不是20070524_1.html
                        //        strNumber = strNumber + " [" + "" + m + "" + "] ";
                        //    else
                        //    {
                        //        int n = m - 1;//第三页的连接应该是20070524_2.shtml,以此类推
                        //        strNumber = strNumber + " [" + "" + m + "" + "] ";
                        //    }
                        //}

                        动态生成table,要符合模板Template.html的table布局
                        //string strTable = "

upUrlNumberdownUrl
";//上下页(分页)表格,注意此处的upUrl(上一页),Number(页码分页),downUrl(下一页),这三个是用来替换的。

                        //if (upbound == 0)//如果没有分页,就直接按日期时间保存
                        //{
                        //    FilePath = Server.MapPath("../../") + "//" + strDate + ".html";
                        //    strhtml = strhtml.Replace("Pager", ""); //"Pager"为文章分页替换字符
                        //}
                        //else//否则按20070524.html、20070524_1.html 这种效果保存
                        //{
                        //    if (i == 0)
                        //        FilePath = Server.MapPath("test/") + "//" + strDate + ".html";
                        //    else
                        //        FilePath = Server.MapPath("test/") + "//" + strDate + "_" + i + ".html";

                        //    if (i == 0)//第一页不显示“上一页”
                        //        strTable = strTable.Replace("upUrl", ""); //strTable为分页表格的字符串

                        //    if (i <= 1)//上一页分页,显示“上一页”图片
                        //        strTable = strTable.Replace("upUrl", "");
                        //    else
                        //    {
                        //        int p = i - 1; //显示禁用“上一页”图片
                        //        strTable = strTable.Replace("upUrl", "");
                        //    }

                        //    if (upbound == 1) 文章内容content数组的上限,即长度;如果只有一页,则不显示页码
                        //        strTable = strTable.Replace("Number", "");
                        //    else
                        //        strTable = strTable.Replace("Number", strNumber);//页码替换
                        //    /**/
                        //   
                        //    if (i == upbound - 1)//最后一页不显示“下一页”
                        //        strTable = strTable.Replace("downUrl", "");

                        //    if (i != upbound - 1)//下一页分页
                        //    {
                        //        int q = i + 1;
                        //        strTable = strTable.Replace("downUrl", "");
                        //    }
                        //    else
                        //    {
                        //        int j = upbound - 1; //显示禁用“下一页”图片
                        //        strTable = strTable.Replace("downUrl", "");
                        //    }

                        //    strhtml = strhtml.Replace("Pager", strTable);
                        //}
                        #endregion

                        #region 不同的分页效果:分页在上
                        string strNumber1 = "";
                        for (int m = 1; m <= upbound; m++)
                        {
                            if (m == 1)//如果是第一页就显示成这个样子:20070524.shtml而不是20070524_1.shtml
                                strNumber1 = strNumber1 + " [" + "" + m + "" + "] ";
                            else
                            {
                                int n = m - 1;//第三页的连接应该是20070524_2.shtml,以此类推
                                strNumber1 = strNumber1 + " [" + "" + m + "" + "] ";
                            }
                        }

                        if (upbound == 0)//如果没有分页,就直接按日期时间保存
                        {
                            FilePath = Server.MapPath("test/") + "//" + strDate + ".html";
                            strhtml = strhtml.Replace("topnum", "");
                        }
                        else//否则按20070524.shtml、20070524_1.shtml 这种效果保存
                        {
                            if (i == 0)
                                FilePath = Server.MapPath("test/") + "//" + strDate + ".html";
                            else
                                FilePath = Server.MapPath("test/") + "//" + strDate + "_" + i + ".html";
                        }

                        strhtml = strhtml.Replace("topnum", strNumber1);
                        strhtml = strhtml.Replace("Pager", Convert.ToString(i+1).Trim());
                        #endregion

                        //创建文件信息对象--------------------------------------------
                        FileInfo finfo = new FileInfo(FilePath);

                        using (FileStream fs = finfo.OpenWrite())//以打开或者写入的形式创建文件流
                        {
                            //根据上面创建的文件流创建写数据流
                            StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
                            //把新的内容写到创建的HTML页面中
                            sw.WriteLine(strhtml);
                            sw.Flush(); //清空
                            sw.Close(); //关闭
                        }
                    }
                    //ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('添加文章成功!');", true);
                    string strNewUrl = "test/" + url;
                    Response.Redirect(strNewUrl);
                }
                catch (Exception err)
                {
                    //输出异常信息
                    Response.Write(err.ToString());
                }
                #endregion
            //}
            //else
            //    ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('添加文章失败!');", true);
        }       
    }
}

==============================================================

网页模板:乱写的,你自己去设计成 div+css





静态html分页例子

 





 
   
   
 
 


 
   
 



 
   
 
 
   
   
   
 
 
   
 
 

   
     
       
     
     
       
     
     
       
     
     
       
     
     
       
     
   
 
Title

         

            作者:strAuthor  | 类别:strType  | 日期:strTime
       

            引用至:strFrom

         
           
         
         
           
         
         
           
         
         
       

               

               

                    第 topnum 页    
               

           
strContent

                 第 Pager 页

               

           
 
 
        广告业务
   

   




 
   
 









 

你可能感兴趣的:(.net,asp.net,string,class,border,function,html)