日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

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

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:從XML文件中讀取數(shù)據(jù)綁定到DropDownList
1 、綁定DropDownList: 以下為引用的內(nèi)容: ddl_language.DataSource = createDataSource(); ddl_language.DataTextField = "languageText

在用 ASP.NET 編程時,打開一個頁面一般是通過指定超鏈接地址,調(diào)用指定的頁面文件(.html、.aspx)等方法。

但是,如果即將打開的頁面文件的內(nèi)容是在程序中動態(tài)生成,或者是從數(shù)據(jù)庫的表里取出的,我們怎么把這些內(nèi)容展示出來呢?
我們最直接的想法是,把這些內(nèi)容先保存成網(wǎng)頁文件,再調(diào)用它。這種方法當然是可以的,但不是最好的方法,因為這樣會在 Web 服務器上生成
許多臨時文件,這些文件可能永遠也用不著了。

另一種最好的方法是利用文本格式流,把頁面內(nèi)容動態(tài)地展示出來。例如,有一個頁面:

以下為引用的內(nèi)容:
……
<iFrame src=""></iframe>
……

需要用 iFrame 打開一個頁面,這個頁面的內(nèi)容是動態(tài)生成的。我們可以寫一個 .ashx 文件(這里命名為 html.ashx)來處理。.ashx 文件里實現(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 對象中,顯示出來。

這樣以來,我們就可以通過 <iFrame src="html.ashx"></iframe> 來展示動態(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ù)庫中得到(事先把一個 html 文件內(nèi)容保存在數(shù)據(jù)庫中)。

分享:C#定時器的使用
C#定時器的使用 以下為引用的內(nèi)容: Timer timer1; this.timer1.Interval = 1000; this.timer1.Tick = new System.Ev

來源:模板無憂//所屬分類:.Net教程/更新時間:2008-08-22
相關(guān).Net教程