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();
}
}
}
}