Asp.net生成静态页(调试通过)

moban.htm(模版页)
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" align="center" border="1">
    <TR>
     <TD height="20"><FONT face="宋体">Asp.net生成Html文件</FONT></TD>
    </TR>
    <TR>
     <TD><FONT face="宋体">HtmlTitle</FONT></TD>
    </TR>
    <TR>
     <TD height="9"><FONT face="宋体">HtmlContent</FONT></TD>
    </TR>
    <TR>
     <TD><FONT face="宋体">HtmlCreateDate</FONT></TD>
    </TR>
    <TR>
     <TD><FONT face="宋体"> </FONT>
     </TD>
    </TR>
   </TABLE>

.aspx文件

<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" cellSpacing="1"
     cellPadding="1" width="300" border="1">
     <TR>
      <TD></TD>
      <TD>生成Html文件</TD>
     </TR>
     <TR>
      <TD>标题:</TD>
      <TD>
       <asp:TextBox id="name" runat="server"></asp:TextBox></TD>
     </TR>
     <TR>
      <TD>内容:</TD>
      <TD>
       <asp:TextBox id="content" runat="server" TextMode="MultiLine" Width="230px" Height="96px"></asp:TextBox></TD>
     </TR>
     <TR>
      <TD></TD>
      <TD>
       <asp:Button id="createButton" runat="server" Text="生 成"></asp:Button>
       <asp:Button id="escButton" runat="server" Text="取 消"></asp:Button></TD>
     </TR>
     <TR>
      <TD></TD>
      <TD></TD>
     </TR>
    </TABLE>

.aspx.cs后台代码

引用命名空间
using System.IO;
using System.Text;

关键代码:
private void createButton_Click(object sender, System.EventArgs e)
  {//生成Html页程式
  string savePath=Server.MapPath("News/");
   if (Directory.Exists(savePath)==false){
   //目录不存在
    Directory.CreateDirectory(savePath);
   }
   Encoding code=Encoding.GetEncoding("gb2312");
   string temp=Server.MapPath("moban.htm");
   StreamReader sr=null;
   StreamWriter sw=null;
   string str="";
   try
   {
    sr=new StreamReader(temp,code);
    str=sr.ReadToEnd();
   }
   catch(Exception ex){
   Response.Write(ex.ToString());
    sr.Close();
   Response.End();
   }
   //获取文件名
   string fileName=DateTime.Now.ToString("yyyyMMddHHmmss")+".htm";
   string temptitle=name.Text;
   string tempcontent=content.Text;
   //过滤恶意字符
   temptitle=temptitle.Replace("'","〃");
   temptitle=temptitle.Replace("<","《");
   temptitle=temptitle.Replace(">","》");
   tempcontent=tempcontent.Replace("'","〃");
   tempcontent=tempcontent.Replace("<","《");
   tempcontent=tempcontent.Replace(">","》");
   //替换空格和回车
   temptitle=temptitle.Replace(" ","&nbsp;");
   tempcontent=tempcontent.Replace(" ","&nbsp;");
   tempcontent=tempcontent.Replace(Convert.ToChar(13).ToString(),"<br>");
   //tempcontent=tempcontent.Replace("/n","<br>");
   //替换标题,内容,生成时间
   str=str.Replace("HtmlTitle",temptitle.ToString());
   str=str.Replace("HtmlContent",tempcontent.ToString());
   str=str.Replace("HtmlCreateDate",DateTime.Now.ToString());
   //执行写入文件
   try
   {
    sw=new StreamWriter(savePath+fileName,false,code);
    sw.Write(str);
    sw.Flush();
    Response.Write("<script>alert('写入文件成功!');location.href='index.aspx'</script>");
   }
   catch(Exception ex)
   {
    Response.Write(ex.ToString());
    Response.End();
   }
   finally{
   sw.Close();
   }
  }

其他IO相关
StreamReader Sr=new StreamReader(Server.MapPath("moban.htm"),Encoding.GetEncoding("gb2312"));
   string str=Sr.ReadToEnd();
   //readText.Text=str.ToString();
   //str.GetEnumerator();
   readText.Text="";
   readText.Text+="/n"+str.IndexOf("<!--HtmlTitleStart-->");//第一个匹配项的索引
   readText.Text+="/n"+str.IndexOf("<!--HtmlTitleEnd-->");
   //Response.Write(str.Insert(str.IndexOf("<!--HtmlTitleStart-->"),"123").ToString());//插入字符
   //str.Length;//获取字符数
   //Response.Write(str.Remove(str.IndexOf("<!--HtmlTitleStart-->"),15));//删除字符
   //获取执行位置的字符
   //Response.Write(str.Substring(str.IndexOf("<!--HtmlTitleStart-->"),str.IndexOf("<!--HtmlTitleEnd-->")-str.IndexOf("<!--HtmlTitleStart-->")));

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值