• asp.net静态生成


    今天看了看asp.net静态生成的视频http://v.youku.com/v_show/id_XMjI3NDk5MzQw.html

      决定自己打出来留着以后用首先、建一个Default.aspx 

    Default.aspx 页面

            <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate><table><tr><td>编号</td><td>名称</td><td></td></tr></HeaderTemplate>
            <ItemTemplate><tr><td><%#Eval("id") %></td><td><%#Eval("name") %></td><td>
                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("id","~/html/{0}.html")%>'>查看详细</asp:HyperLink></td></tr></ItemTemplate>
            <FooterTemplate></table></FooterTemplate>
            </asp:Repeater>
            <asp:Button ID="Button1" runat="server" Text="生成静态页面" onclick="Button1_Click" />

      

    Default.aspx.cs页面

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.OleDb;
    using System.IO;                                //File空间引用


    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }
        /// <summary>
        /// 绑定数据库
        /// </summary>
        public void Bind()
        {
            string conn = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/database.mdb");
            OleDbConnection con = new OleDbConnection(conn);

            string str = "select * from news";

            OleDbCommand com = new OleDbCommand(str, con);
            DataSet ds = new DataSet();

            con.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(com);
            da.Fill(ds);
            con.Close();
            //绑定到Repeater上
            Repeater1.DataSource = ds;
            Repeater1.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            //<!--链接字符串
            string conn = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/database.mdb");
            OleDbConnection con = new OleDbConnection(conn);
            string tr = "select * from news";

            OleDbCommand com = new OleDbCommand(tr, con);
            DataSet ds = new DataSet();

            con.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(com);
            da.Fill(ds);
            con.Close();
            //-->

            string path =Server.MapPath("~/html/");                        //带读取html模版的路径
            string temp = File.ReadAllText(path + "HTMLPage.htm",System.Text.Encoding.UTF8);          //读取模版文件所有内容,和编码

            foreach (DataRow dr in ds.Tables[0].Rows)                       //循环读取ds的内容
            {
                string content = temp.Replace("$name$", dr["name"].ToString()); //替换模板页内容  content现在存的是html代码
                content = content.Replace("$time$", dr["time"].ToString());
                content = content.Replace("$body$", dr["body"].ToString());
                File.WriteAllText(path + dr["id"] + ".html", content, System.Text.Encoding.UTF8);   //在制定位置创建文件,内容,编码
            }
        }
    }

    HTMLPage.htm  html模板页

    <html>
    <head runat="server">
        <title>无标题</title>
    </head>
    <body>
    <form>
    <!--这是静态模板-->
    <div>标题:$name$
    <br />
        时间:$time$<br />
        <hr />
        $body$
    </div>
    </form>
    </body>
    </html>

    OK   点击生成静态页面按钮、、就可以看到了、、具体样式 具体添加

    带Repeater   aspx页

     <asp:Repeater ID="Repeater1" runat="server">
            <HeaderTemplate><table><tr><td>编号</td><td>名称</td><td></td></tr></HeaderTemplate>
            <ItemTemplate><tr><td><%#Eval("id") %></td><td><%#Eval("name") %></td><td>
                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("id","~/html/{0}.html")%>'>查看详细</asp:HyperLink></td></tr></ItemTemplate>
            <FooterTemplate></table></FooterTemplate>
    </asp:Repeater>

    点击生成按钮

        protected void Button2_Click(object sender, EventArgs e)
        {
            //<!--链接字符串
            string conn = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/database.mdb");
            OleDbConnection con = new OleDbConnection(conn);
            string tr = "select * from news";

            OleDbCommand com = new OleDbCommand(tr, con);
            DataSet ds = new DataSet();

            con.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(com);
            da.Fill(ds);
            con.Close();
            //-->
            string path = Server.MapPath("~/html/");                        //带读取html模版的路径
            string temp = File.ReadAllText(path + "HTMLPage2.htm", System.Text.Encoding.UTF8);          //读取模版文件所有内容,和编码
            string body = "<table><tr><td>编号</td><td>名称</td><td></td></tr>";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                body += "<tr><td>"
                + dr["id"] + "</td><td>"
                + dr["name"] + "</td><td><a href='../html/"
                + dr["id"] + ".html'>查看详细</a></td></tr>";           
            }
            body+="</table>";
            string cont = temp.Replace("$body$", body);
            File.WriteAllText(path + "Def.html", cont, System.Text.Encoding.UTF8);
        }
    }

    htm模板


    <html>
    <head>
        <title></title>
    </head>
    <body>
    $body$
    </body>
    </html>

  • 相关阅读:
    查看dll定义的宏
    循环调用dll库的界面时,首次正常,再次无响应
    strftime使用%F格式化日期失败
    自己程序生成的二维码部分手机无法识别
    Drools规则
    idea快捷键
    风控文档笔记
    工作笔记
    工具方法
    BIO,NIO,AIO
  • 原文地址:https://www.cnblogs.com/taikongbai/p/2580106.html
Copyright © 2020-2023  润新知