ASP.NET静态页生成技术

using System;
using System.Data;
using System.Configuration;
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.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            Bind1();
        }
    }
    private void Bind()
    {
        SqlConnection con = new SqlConnection("server=.;database=htmls;uid=sa;pwd=sa;");
        string sql = "select * from html";
        SqlCommand cmd = new SqlCommand(sql,con);
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        sda.Fill(ds,"html");
        GridView1.DataSource=ds.Tables["html"];
        GridView1.DataBind();
    }

    private void Bind1()
    {
        SqlConnection con = new SqlConnection("server=.;database=htmls;uid=sa;pwd=sa;");
        string sql = "select * from html";
        SqlCommand cmd = new SqlCommand(sql, con);
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        sda.Fill(ds, "html");
        Repeater1.DataSource = ds.Tables["html"];
        Repeater1.DataBind();
    }

    //模板替换法
    protected void Button1_Click(object sender, EventArgs e)
    {
        string path = Server.MapPath("~/html/modo.htm");
        if (File.Exists(path))
        {
            StreamReader sr = new StreamReader(path,Encoding.UTF8);
            string modoValue = sr.ReadToEnd();
            sr.Close();

            SqlConnection con = new SqlConnection("server=.;database=htmls;uid=sa;pwd=sa;");
            string sql = "select * from html";
            SqlCommand cmd = new SqlCommand(sql, con);
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds, "html");
            dt=ds.Tables["html"];
            foreach (DataRow row in dt.Rows)
            {
                StringBuilder result = new StringBuilder(modoValue);
                result = result.Replace("{@title}",row["title"].ToString());
                result = result.Replace("{@neirong}", row["neirong"].ToString());
                result = result.Replace("{@times}", row["times"].ToString());
                string uid = row["id"].ToString();
                string spath=Server.MapPath("~/test");
                if (!Directory.Exists(spath))
                {
                    Directory.CreateDirectory(spath);
                }
                string savepath = Server.MapPath("~/test/show"+uid+".html");
                StreamWriter write = new StreamWriter(savepath,false,Encoding.UTF8);
                write.Write(result.ToString());
                write.Close();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值