免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
asp.net生成html靜態(tài)頁的多種方法
用C#做腳本的asp.net的方法,這個是我自己寫的,在《Visual C#.NET范例入門與提高》的P311,有對WebRequest和HttpRequest、HttpWebRequest、HttpWebResponse四個類的簡單說明
private bool CreateList(string url, string fna)
    {
        bool ok;
        //準備生成
        string strHtml;
        StreamReader sr = null; //用來讀取流
        StreamWriter sw = null; //用來寫文件
        Encoding code = Encoding.GetEncoding("utf-8"); //定義編碼

        //構造web請求,發(fā)送請求,獲取響應
        WebRequest HttpWebRequest = null;
        WebResponse HttpWebResponse = null;
        HttpWebRequest = WebRequest.Create(url);
        HttpWebResponse = HttpWebRequest.GetResponse();

        //獲得流
        sr = new StreamReader(HttpWebResponse.GetResponseStream(), code);
        strHtml = sr.ReadToEnd();

        //寫入文件
        try
        {
            sw = new StreamWriter(fna, false, code);
            sw.Write(strHtml);
            sw.Flush();
            ok = true;
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("<p>寫入文件出錯:" + ex.Message);
            HttpContext.Current.Response.End();
            ok = false;
        }
        finally
        {
            sw.Close();
        }
        return ok;
    }

調用的時候這樣:

//要生成html頁面的aspx頁面
        string url = @"http://localhost/list.aspx";
        //html頁面文件名
        string fna = Server.MapPath("") + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString() + ".html";
        if (CreateList(url, fna))
        {
            Response.Write("<p>生成文件成功:" + fna);
        }

第二種方法是用一個html模板生成一個html頁面,模版里面有對應的標簽,可以從數(shù)據(jù)庫和別的地方取數(shù)據(jù),填寫這個標簽,生成一個html頁面,這個方法在很多新聞系統(tǒng)里有用到

我參考這里面的代碼寫的:http://www.webstudy8.com/web/net/201/065118272748882.html

private string CreateDetailPage(string EventID,string EventTitle, string EventBody, string EventTime, string EventStat)
    {
        //模版所有路徑、模版文件名、生成的文件路徑、生成的文件名
        string path, temp, htmlfilepath,htmlfilename;
        path = Server.MapPath("");
        temp = Server.MapPath("testhtml.htm");
        htmlfilepath = path;
        htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString() + ".html";

        //讀模版
        Encoding code = Encoding.GetEncoding("gb2312");

        StreamReader sr = null;
        StreamWriter sw = null;
        string str = "";

        try
        {
            sr = new StreamReader(temp, code);
            str = sr.ReadToEnd(); // 讀取文件
        }
        catch (Exception exp)
        {
            HttpContext.Current.Response.Write("<p>讀取文件出錯:" + exp.Message);
            HttpContext.Current.Response.End();
            sr.Close();
        }

        // 替換內容
        // 對應模版里的設置要修改
        str = str.Replace("re_symbol_EventID", EventID);
        str = str.Replace("re_symbol_EventTitle", EventTitle);
        str = str.Replace("re_symbol_EventBody", EventBody);
        str = str.Replace("re_symbol_EventTime", EventTime);
        str = str.Replace("re_symbol_EventStat", EventStat);

        // 寫文件
        try
        {
            sw = new StreamWriter(htmlfilepath + "\\" + htmlfilename, false, code);
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("<p>寫入文件出錯:" + ex.Message);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sw.Close();
        }
        return htmlfilename;
    }

調用的時候這樣:

//取內容,這里我取了頁面上的一個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);
本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C# winform DevExpress GridControl GridView大批量數(shù)據(jù)(20萬條)導出Excel
string str=null與 string str=""區(qū)別
ASP.NET生成HTML初級解決方案
生成靜態(tài)頁
GridView中的數(shù)據(jù)導出到Excel方法(經(jīng)測試完整)
strtok
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服