protected void gridview1_rowdeleting(object sender, gridviewdeleteeventargs e)
{
string n_id = this.gridview1.datakeys[e.rowindex]["nid"].tostring();
sqlconnection conn = new sqlconnection("server=.;uid=sa;pwd=;database=dress");
sqlcommand cmd = new sqlcommand("select nid,n_title,n_content,n_pic,n_date from p_news where nid="+n_id,conn);
conn.open();
sqldatareader dr = cmd.executereader();
//擷取新聞發布的時間
string date = "";
if(dr.read())
{
date = convert.todatetime(dr["n_date"]).tostring("yyyymmddhhmmss_");
//調用靜態產生的方法
transferstatic(dr["n_title"].tostring(), dr["n_content"].tostring(), dr["n_pic"].tostring(), date, n_id);
response.write("<script>alert('靜態頁面產生成功!');location='bulidstaticpage.aspx'</script>");
}
}
//轉換靜態方法
public bool transferstatic(string title,string content,string pic,string date,string nid)
{
//輸出路徑
string outpath = server.mappath("~/newdetails");
//簡體中文
encoding encoding = encoding.getencoding("gb2312");
//讀模數版檔案
string htmlmodel = server.mappath("~/htmlmodel/newdetail.html");
streamreader sr = null;
streamwriter sw = null;
string str = "";//儲存內容的字串
try
{
sr=new streamreader(htmlmodel,encoding);
str=sr.readtoend();//從頭讀到尾
}
catch(exception e)
{
response.write(e.message);
response.end();
sr.close();
}
//刪除指定的頁面
protected void button2_click(object sender, eventargs e)
{
string path = server.mappath("~/newdetails");
file.delete(path + "//" + this.listbox1.selectedvalue);
//page.clientscript.registerclientscriptblock(this.gettype(), "資訊處理", "<script>alert('刪除成功!');location='bulidstaticpage.aspx'</script>");
page.clientscript.registerstartups教程cript(this.gettype(), "資訊處理", "<script>alert('刪除成功!');location='bulidstaticpage.aspx'</script>");
}
產生靜態頁列表
//得到靜態頁面的路徑
public string htmlpath(datetime date,string nid)
{
string path = "newdetails/" + date.tostring("yyyymmddhhmmss_") + nid + ".html";
return path;
}
//產生靜態列表
protected void button1_click(object sender, eventargs e)
{
sqlconnection conn = new sqlconnection("server=.;uid=sa;pwd=;database=dress");
sqldataadapter da = new sqldataadapter("select nid,n_title,n_content,n_pic,n_date from p_news",conn);
dataset ds = new dataset();
da.fill(ds);
string str = "";
for (int i = 0; i < ds.tables[0].rows.count;i++)
{
//取時間
string date = convert.todatetime(ds.tables[0].rows[i]["n_date"]).tostring("yyyymmddhhmmss_");
//取編號
string nid = ds.tables[0].rows[i]["nid"].tostring();
//構造檔案名稱
string pagename = date + nid + ".html";
//構造字串
str += "<tr><td width=360px>"+"<a href='newdetails/"+pagename+"'>"+ds.tables[0].rows[i]["n_title"].tostring()+"</a>"+"</td><td width=200px>"+ds.tables[0].rows[i]["n_date"].tostring()+"</td></tr>";
}
//調用轉換方法產生靜態頁
transferstatic(str);
}
//產生靜態頁
public bool transferstatic(string strall)
{
//輸出路徑
string outpath = server.mappath("~/");
encoding encoding = encoding.getencoding("gb2312");
//讀模數版檔案
string htmlmode = server.mappath("~/htmlmodel/newslist.html");
streamreader sr = null;
streamwriter sw = null;
string str = "";
try
{
sr = new streamreader(htmlmode, encoding);
str = sr.readtoend();
}
catch (exception e)
{
response.write(e.message);
response.end();
sr.close();
}
//構造要產生的靜態頁面的名字
string pagename = "newslist.html";
//開始替換內容
str = str.replace("newslist", strall);
//寫檔案
try
{
sw = new streamwriter(outpath + pagename, false, encoding);
sw.write(str);//將字串str寫到檔案
sw.flush();
}
catch (exception e)
{
response.write(e.message);
response.end();
}
finally
{
sw.close();
}
return true;
}
在前台綁定新聞標題和時間:
<table>
<asp:repeater id="repeater1" runat="server">
<itemtemplate>
<tr>
<td width="360px"><a href='<%# htmlpath(convert.todatetime(eval("n_date")),eval("nid").tostring())%>'><%# eval("n_title") %></a></td>
<td width="200px"><%# eval("n_date") %></td>
</tr>
</itemtemplate>
</asp:repeater>