天天看点

精彩:Asp.net动态生成html页面(转自太平洋电脑网)

????姝ゅ???介???ㄤ????版?版??搴????戒?寮虹??web绔??癸??冲ぇ?ㄥ??????涓???瀛??惧?ㄦ?版??搴???璁板?涓?锛??????惧??tml??浠舵????xml??浠朵腑锛?浠?浠???绱㈠??惧?版?版??搴?涓?锛?濡???绔???棰???绫诲?????ヨ?㈠?抽??瀛?绛???杩??烽????浜????版病??璇稿?MS Sql Server杩??风???版??搴???????Web绔??广??

???????ㄤ??伴?诲??甯?绯荤?锛?姣?濡?sina??163绛??芥?????ㄥ?ㄦ??????html椤甸?㈢????

???????ㄤ????ㄦ??瀹??堕〉?㈢??绋?搴???姣?濡?璁哄??????澶╁?ょ??????浠ュ??杞藉???跺ソ??html椤甸????ュ??寮虹?瑙???

??????璺?

????1. ?╃?ㄥ?Dw-Mx杩??风??宸ュ?风????html?煎???妯℃?匡??ㄩ??瑕?娣诲???煎????版?瑰???ョ?规????璁?濡?$htmlformat$),?ㄦ????????浠舵?跺?╃?ㄤ唬??璇诲??姝ゆā?匡??跺???峰????拌??ョ????瀹癸?娣诲???版?ゆā?跨????璁颁?缃?涓?锛??????版??浠跺???????ョ???锛????ュ???????版??搴?涓????ョ?稿?虫?版????

????2. 浣跨?ㄥ???颁唬??纭?缂???Html??浠讹???浠ヤ娇??tmlTextWriter绫绘?ュ??html??浠躲??

????浼???

????1. ??浠ュ缓绔???甯稿?????椤甸????╃?ㄥ????js??浠剁???规?锛???s??浠跺??????ocument.write()?规???浠ュ?ㄦ????椤甸?㈠?????ュ?椤甸?㈠ご锛?骞垮??绛???瀹广??

????2. ????html??浠跺?╃??S Windows2000??Index Server??浠ュ缓绔??ㄦ????绱㈠???锛??╃??sp.net??浠ヤ互DataTable???瑰?寰??版??绱㈢???????Win2000??Index???℃??娉??ユ??ml??浠剁????瀹广??濡???????浜??版??搴???绱??Index绱㈠??????ユ?撅??d?姝ゆ??绱㈠???藉???甯稿己澶с??

????3. ???????″?ㄧ??璐??凤?璇锋?涓?涓???????html??浠舵??涓?涓?aspx??浠舵???″?ㄨ?婧?????璁稿???

????缂虹??

??????璺?浜?锛? 濡????ㄧ‖缂??????瑰?锛?宸ヤ?????甯稿ぇ锛???瑕???甯稿???html浠g????璋?璇??伴?俱????涓?浣跨?ㄧ‖缂?????????html?峰???娉?淇??癸?濡???缃?绔??存?㈡?峰?锛??d?蹇?椤诲????扮???锛?缁?????甯??ュ法澶х??宸ヤ?????

??????姝よ??????ㄧ????绗?涓?绉???璺?

????绀哄??浠g??

????1.瀹?涔?(template.htm)html妯℃?块〉??

????<html>

????<head>

????<title></title>

????<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

????</head>

????<body >

????<table $htmlformat[0] height="100%" width="100%" cellpadding="10" cellspacing="0" bgcolor="#eeeeee" style="border:1px solid #000000">

????<tr>

????<td width="100%" valign="middle" align="left">

????<span style="color: $htmlformat[1];font-size: $htmlformat[2]">$htmlformat[3]</span>

????</td>

????</tr>

????</table>

????</body>

????</html>

2.asp.net浠g??锛?

????//---------------------璇?tml妯℃?块〉?㈠??tringbuilder瀵硅薄??----

????string[] format=new string[4];//瀹?涔???htmlyem??璁版?扮??涓??寸???扮?

????StringBuilder htmltext=new StringBuilder();

????try

????{

????using (StreamReader sr = new StreamReader("瀛??炬ā?块〉?㈢??璺?寰???椤甸?㈠??"))

????{

????String line;

????while ((line = sr.ReadLine()) != null)

????{

????htmltext.Append(line);

????}

????sr.Close();

????}

????}

????catch

????{

????Response.Write("<Script>alert('璇诲????浠堕??璇?')</Script>");

????}

????//---------------------缁???璁版?扮?璧???-----------

????format[0]="background=/"bg.jpg/"";//?????剧??

????format[1]= "#990099";//瀛?浣?棰???

????format[2]="150px";//瀛?浣?澶у?

????format[3]= "<marquee>??????妯℃??tml椤甸??lt;/marquee>";//??瀛?璇存??

????//----------?挎??tm??????璁颁负浣??冲??????瀹?

????for(int i=0;i<4;i++)

????{

????htmltext.Replace("$htmlformat["+i+"]",format[i]);

????}

????//----------????htm??浠?-----------------????

????try

????{

????using(StreamWriter sw=new StreamWriter("瀛??捐矾寰???椤甸?㈠??",false,System.Text.Encoding.GetEncoding("GB2312")))

????{

????sw.WriteLine(htmltext);

????sw.Flush();

????sw.Close();

????}

????}

????catch

????{

????Response.Write ("The file could not be wirte:");

????}

????灏?缁?

?????ㄦ?ゆ?规???浠ユ?逛究??????html??浠躲??绋?搴?浣跨?ㄤ???寰????挎?????姝ゅ?归???挎?㈠ぇ????绱???妯℃?块??搴???甯稿揩??