asp.net實現產生靜態頁並添加連結的方法_實用技巧

來源:互聯網
上載者:User

本文以執行個體講解了asp.net實現產生靜態頁並添加連結的方法,非常實用的功能,通過本執行個體可以加深讀者對於asp.net下檔案操作的認識。

1.建立一個靜態網頁模板

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>模板網頁</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><table $htmlformat[0] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" gcolor="#eeeeee" style="border:1px solid #000000"><tr><td width="100%" valign="middle" align="left"><h1>$htmlformat[1]</h1><div style="color: $htmlformat[2];font-size: $htmlformat[3]">$htmlformat[4]</div></td></tr></table></body></html>

2.在asp.net網頁後台代碼中替換模板html頁中的標記符

protected void Button1_Click(object sender, EventArgs e){string[] format = new string[5];//定義和htmlyem標記數目一致的數組StringBuilder htmltext = new StringBuilder();string templatePath = Server.MapPath("~/html/Template.html");StreamReader sr = new StreamReader(templatePath);String line;while ((line = sr.ReadLine()) != null){htmltext.Append(line);}sr.Close();//---------------------給標記數組賦值------------format[0] = "background=\"bg.jpg\"";//背景圖片format[1] = TB_Title.Text;format[2] = "#990099"; //字型顏色format[3] = "60px";//文字大小format[4] = TB_Content.Text;//----------替換htm裡的標記為你想加的內容for (int i = 0; i < 5; i++){htmltext.Replace("$htmlformat[" + i + "]", format[i]);}//----------產生htm檔案------------------――string newfile = Server.MapPath("~/html/"+TB_Title.Text+".html");StreamWriter sw = new StreamWriter(newfile, false, System.Text.Encoding.GetEncoding("GB2312"));sw.WriteLine(htmltext);sw.Flush();sw.Close();string newhref="html/" rel="external nofollow" +TB_Title.Text+".html";Response.Write("<a href=\"" + newhref + "\">" + TB_Title.Text + "</a>");}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.