本文以實例講解了asp.net實現(xiàn)生成靜態(tài)頁并添加鏈接的方法,非常實用的功能,通過本實例可以加深讀者對于asp.net下文件操作的認識。
1.創(chuàng)建一個靜態(tài)網(wǎng)頁模板
!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>模板網(wǎng)頁/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網(wǎng)頁后臺代碼中替換模板html頁中的標記符
protected void Button1_Click(object sender, EventArgs e)
{
string[] format = new string[5];//定義和htmlyem標記數(shù)目一致的數(shù)組
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();
//---------------------給標記數(shù)組賦值------------
format[0] = "background=\"bg.jpg\"";//背景圖片
format[1] = TB_Title.Text;
format[2] = "#990099"; //字體顏色
format[3] = "60px";//文字大小
format[4] = TB_Content.Text;
//----------替換htm里的標記為你想加的內(nèi)容
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>");
}
您可能感興趣的文章:- asp.net中MVC借助Iframe實現(xiàn)無刷新上傳文件實例
- asp.net在iframe中彈出信息并執(zhí)行跳轉(zhuǎn)問題探討
- ASP.NET頁面借助IFrame提交表單數(shù)據(jù)所遇到問題的解決方法分享
- asp.net 文件上傳與刷新與asp.net頁面與iframe之間的數(shù)據(jù)傳輸
- ASP.NET中使用IFRAME建立類Modal窗口
- asp.net省市三級聯(lián)動的DropDownList+Ajax的三種框架(aspnet/Jquery/ExtJs)示例
- asp.net 學習之路 項目整體框架簡單的搭建
- asp.net GridView中超鏈接的使用(帶參數(shù))
- ASP.NET 鏈接 Access 數(shù)據(jù)庫路徑問題最終解決方案
- ASP.NET中iframe框架點擊左邊頁面鏈接 右邊顯示鏈接頁面內(nèi)容