【www.gdgbn.com--页面特效】

asp教程.net生成静态页面并分页



   
    title


   
       

        title
   
   
        浏览

            <script src="http://localhost/.net/newsfiles/clickcount.aspx?newsid=newsid"> </script>

        次 time
   
   
        content
   
   
        pager
   
   
       


           
           
           

           
           

           
           
            http://www.0379zd.com/news/display.aspx?newsid=newsid">查看更多评论
       

   


 

2、前态页面 newsadd.aspx,就是一个表单,用来填写新闻的标题和内容。


代码
<%@ page language="c#" autoeventwireup="false" validaterequest="false" codefile="newsadd.aspx.cs" inherits="newsadd.admin_adminpanel_newsadd" %>

<%@ register tagprefix="fckeditorv2" namespace="fredck.fckeditorv2" assembly="fredck.fckeditorv2" %>



    添加新闻


   


       
           
           
           

           
           
           

           
           
       
   



 

3、后台页面 newsadd.aspx.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 mysql教程server;
using system.io;
using system.text;
namespace newsadd
{
    public partial class admin_adminpanel_newsadd : system.web.ui.page
    {
        protected void page_load(object sender, eventargs e)
        {

        }
        protected void button1_click(object sender, eventargs e)
        {
            string strdate = datetime.now.tostring("yymmdd") + "" + datetime.now.tostring("yyyymmddhhmmss");
            string strfilename = strdate + ".shtml";//存储到数据库教程中
            string strtitle = request.form["title"].tostring().trim();//接收传过来的标题
            string strcontent = request.form["content"].tostring().trim();//接收传过来的内容
            string[] content = strcontent.split(new char[] { "|" });//对内容进行拆分,并保存到数组
            int upbound = content.length;//数组的上限
            sqlserverdatabase db = new sqlserverdatabase();
            bool success = db.insert("insert into innews(title,content,filepath)values("" + strtitle + "","" + strcontent + "","" + strfilename + "")", null);
            //if (success)
            // message.text = "添加成功!";
            /**/
            ///////////////////////////创建当前日期的文件夹开始
            string dir = server.mappath("http://www.cnblogs.com/" + "newsfiles/" + datetime.now.tostring("yymmdd"));//用来生成文件夹
            if (!directory.exists(dir))
            {
                directory.createdirectory(dir);
            }
            /**/
            ///////////////////////////创建当前日期的文件夹结束
            try
            {
                for (int i = 0; i < content.length; i++)
                {
                    //string[] newcontent = new string[4];//定义和html标记数目一致的数组
                    stringbuilder strhtml = new stringbuilder();

                    //创建streamreader对象
                    using (streamreader sr = new streamreader(server.mappath("http://www.cnblogs.com/" + "newsfiles/") + "template.html", encoding.getencoding("gb2312")))
                    {
                        string oneline;
                        //读取指定的html文件模板
                        while ((oneline = sr.readline()) != null)
                        {
                            strhtml.append(oneline);
                        }
                        sr.close();
                    }

                    //为标记数组赋值
                    //sqlserverdatabase db = new sqlserverdatabase();
                    dataset ds = db.select("select top 1 newsid from innews order by newsid desc", null);//获取id
                    string strtable = " upurl number downurl ";//上下页表格,注意此处的upurl(上一页),number(页码分页),downurl(下一页)
                    //这三个是用来替换的。

                    string filepath = "";
                    strhtml = strhtml.replace("title", strtitle);
                    strhtml = strhtml.replace("newsid", ds.tables[0].rows[0]["newsid"].tostring());
                    strhtml = strhtml.replace("time", datetime.now.tostring("yyyy/mm/dd"));
                    strhtml = strhtml.replace("content", content[i]);
                    string strnumber = "";//数字分页1,2,3……
                    for (int m = 1; m <= upbound; m++)
                    {
                        if (m == 1)//如果是第一页就显示成这个样子:20070524.shtml而不是20070524_1.shtml
                            strnumber = strnumber + " [" + " " + m + " " + "] ";
                        else
                        {
                            int n = m - 1;//第三页的连接应该是20070524_2.shtml,以此类推
                            strnumber = strnumber + " [" + " " + m + " " + "] ";
                        }
                    }
                    if (upbound == 0)//如果没有分页,就直接按日期时间保存
                    {
                        filepath = server.mappath("http://www.cnblogs.com/") + "newsfiles" + "//" + strdate + ".shtml";
                        strhtml = strhtml.replace("pager", "");
                    }
                    else//否则按20070524.shtml、20070524_1.shtml 这种效果保存
                    {
                        if (i == 0)
                            filepath = server.mappath("http://www.cnblogs.com/") + "newsfiles" + "//" + strdate + ".shtml";
                        else
                            filepath = server.mappath("http://www.cnblogs.com/") + "newsfiles" + "//" + strdate + "_" + i + ".shtml";

                        if (i == 0)//第一页不显示上一页
                            strtable = strtable.replace("upurl", "");

                        if (i <= 1)//上一页分页
                            strtable = strtable.replace("upurl", " 上一页 ");
                        else
                        {
                            int p = i - 1;
                            strtable = strtable.replace("upurl", " 上一页 ");
                        }

                        if (upbound == 1)//如果只有一页,则不显示页码
                            //strnumber="";
                            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);
                    }
                    //创建文件信息对象--------------------------------------------
                    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();
                    }
                }
            }
            catch (exception err)
            {
                //输出异常信息
                response.write(err.tostring());
            }
        }
    }
}

本文来源:http://www.gdgbn.com/wangyetexiao/29656/