|
用C#做腳本的asp.net的方法,這個(gè)是我自己寫的,在《Visual C#.NET范例入門與提高》的P311,有對(duì)WebRequest和HttpRequest、HttpWebRequest、HttpWebResponse四個(gè)類的簡(jiǎn)單說(shuō)明
private bool CreateList(string url, string fna)
{ bool ok; //準(zhǔn)備生成 string strHtml; StreamReader sr = null; //用來(lái)讀取流 StreamWriter sw = null; //用來(lái)寫文件 Encoding code = Encoding.GetEncoding("utf-8"); //定義編碼 //構(gòu)造web請(qǐng)求,發(fā)送請(qǐng)求,獲取響應(yīng) //獲得流 //寫入文件 調(diào)用的時(shí)候這樣: //要生成html頁(yè)面的aspx頁(yè)面
string url = @"http://localhost/list.aspx"; //html頁(yè)面文件名 string fna = Server.MapPath("") + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString() + ".html"; if (CreateList(url, fna)) { Response.Write("<p>生成文件成功:" + fna); } 第二種方法是用一個(gè)html模板生成一個(gè)html頁(yè)面,模版里面有對(duì)應(yīng)的標(biāo)簽,可以從數(shù)據(jù)庫(kù)和別的地方取數(shù)據(jù),填寫這個(gè)標(biāo)簽,生成一個(gè)html頁(yè)面,這個(gè)方法在很多新聞系統(tǒng)里有用到 我參考這里面的代碼寫的:http://www./web/net/201/065118272748882.html private string CreateDetailPage(string EventID,string EventTitle, string EventBody, string EventTime, string EventStat) //讀模版 StreamReader sr = null; try // 替換內(nèi)容 // 寫文件 調(diào)用的時(shí)候這樣: //取內(nèi)容,這里我取了頁(yè)面上的一個(gè)gridview里的選中行的數(shù)據(jù)
int i; i = GridView1.SelectedIndex; if (i == null || i==-1) i = 0; string EventID, EventTitle, EventBody, EventTime, EventStat; EventID=GridView1.Rows[i].Cells[0].Text; EventTitle=GridView1.Rows[i].Cells[1].Text; EventBody=GridView1.Rows[i].Cells[2].Text; EventTime=GridView1.Rows[i].Cells[3].Text; EventStat=GridView1.Rows[i].Cells[4].Text; //生成文件,返回文件名 string fna; fna=CreateDetailPage(EventID, EventTitle, EventBody, EventTime, EventStat); Response.Write("<p>生成文件成功:" + fna); |
|
|