推薦:從XML文件中讀取數(shù)據(jù)綁定到DropDownList1 、綁定DropDownList: 以下為引用的內(nèi)容:ddl_language.DataSource = createDataSource(); ddl_language.DataTextField = "languageText
在用 ASP.NET 編程時(shí),打開一個(gè)頁面一般是通過指定超鏈接地址,調(diào)用指定的頁面文件(.html、.aspx)等方法。
但是,如果即將打開的頁面文件的內(nèi)容是在程序中動(dòng)態(tài)生成,或者是從數(shù)據(jù)庫的表里取出的,我們?cè)趺窗堰@些內(nèi)容展示出來呢?
我們最直接的想法是,把這些內(nèi)容先保存成網(wǎng)頁文件,再調(diào)用它。這種方法當(dāng)然是可以的,但不是最好的方法,因?yàn)檫@樣會(huì)在 Web 服務(wù)器上生成
許多臨時(shí)文件,這些文件可能永遠(yuǎn)也用不著了。
另一種最好的方法是利用文本格式流,把頁面內(nèi)容動(dòng)態(tài)地展示出來。例如,有一個(gè)頁面:
| 以下為引用的內(nèi)容: …… <iFrame src=""></iframe> …… |
需要用 iFrame 打開一個(gè)頁面,這個(gè)頁面的內(nèi)容是動(dòng)態(tài)生成的。我們可以寫一個(gè) .ashx 文件(這里命名為 html.ashx)來處理。.ashx 文件里實(shí)現(xiàn)了 IHttpHandler 接口類,可以直接生成瀏覽器使用的數(shù)據(jù)格式。
html.ashx 文件內(nèi)容:
| 以下為引用的內(nèi)容: <%@ WebHandler Language="C#" Class="Handler" %> using System; public class Handler : IHttpHandler { public bool IsReusable { public void ProcessRequest (HttpContext context) Stream stream = null; string html = "<html><body>成功: test of txt.ashx</body></html>"; stream = new MemoryStream(html2bytes); if (stream == null) //Write text stream to the response stream } |
html.ashx 文件中首先把 string 字符串轉(zhuǎn)化為字節(jié)(byte)數(shù)組,然后再生成內(nèi)存中的 MemoryStream 數(shù)據(jù)流,最后寫到 OutputStream 對(duì)象中,顯示出來。
這樣以來,我們就可以通過 <iFrame src="html.ashx"></iframe> 來展示動(dòng)態(tài)生成的頁面,顯示“成功: test of txt.ashx”的網(wǎng)頁內(nèi)容。html.ashx 文件中 string html = "<html><body>成功: test of txt.ashx</body></html>"; 一句中,變量 html 的內(nèi)容完全可以從數(shù)據(jù)庫中得到(事先把一個(gè) html 文件內(nèi)容保存在數(shù)據(jù)庫中)。
分享:C#定時(shí)器的使用C#定時(shí)器的使用 以下為引用的內(nèi)容: Timer timer1; this.timer1.Interval = 1000; this.timer1.Tick = new System.Ev
新聞熱點(diǎn)
疑難解答
圖片精選