国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET 2.0 里輸出文本格式流_.Net教程

2024-07-10 12:49:59
字體:
供稿:網(wǎng)友

推薦:從XML文件中讀取數(shù)據(jù)綁定到DropDownList
1 、綁定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;
using System.IO;
using System.Web;

public class Handler : IHttpHandler {

public bool IsReusable {
get {
return true;
}
}

public void ProcessRequest (HttpContext context)
{
// Set up the response settings
context.Response.ContentType = "text/html";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;

Stream stream = null;

string html = "<html><body>成功: test of txt.ashx</body></html>";
byte[] html2bytes = System.Text.Encoding.ASCII.GetBytes(html);

stream = new MemoryStream(html2bytes);

if (stream == null)
stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes("<html><body>get Nothing!</body></html>"));

//Write text stream to the response stream
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
int count = stream.Read(buffer, 0, buffersize);
while (count > 0)
{
context.Response.OutputStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, buffersize);
}
}

}

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 崇州市| 沛县| 任丘市| 宜阳县| 渭源县| 铁力市| 通州区| 建昌县| 泸水县| 奉节县| 诏安县| 肇东市| 阳东县| 交口县| 新闻| 兴文县| 临泽县| 黔西县| 儋州市| 庐江县| 玉田县| 和林格尔县| 西充县| 霍邱县| 南充市| 广平县| 淳安县| 岳阳市| 启东市| 舟山市| 苏尼特右旗| 安图县| 常德市| 西乌珠穆沁旗| 崇礼县| 大同县| 德清县| 阳朔县| 甘孜县| 台东市| 长乐市|