读书人

asp.net生成静态页出现的有关问题。

发布时间: 2011-12-27 22:22:55 作者: rapoo

asp.net生成静态页出现的问题。高手请进。
Conn的类:
using System;
using System.Text;
using System.Web;
using System.IO;
namespace myservers
{
/// <summary>
/// Conn 的摘要说明。
/// </summary>
public class Conn
{
public Conn()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

public bool WriteFile(string strText,string strContent)
{
string path = HttpContext.Current.Server.MapPath( "news/ ");//定义html文件存放路径
Encoding code = Encoding.GetEncoding( "gb2312 ");//定义文字编码
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath( "text.html ");
StreamReader sr=null;
StreamWriter sw=null;
string str= " ";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)


{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlfilename=path + DateTime.Now.ToString( "yyyyMMddHHmmss ")+ ".html ";

str = str.Replace( "title ",strText);
str = str.Replace( "content1 ",strContent);

// 写文件
try
{
sw = new StreamWriter(htmlfilename,false,code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
}
}
}


Add_News.aspx


protected void btnAdd_Click(object sender, ImageClickEventArgs e)
{

Conn H = new Conn();

if (H.Conn.WriteFilethis.Title.Text.ToString),this.content1.Text.ToString))

{
Response.Write( "添加成功 ");
}
else
{
Response.Write( "生成HTML出错! ");
}


}


错误信息:
编译器错误信息: CS1525: 无效的表达式项“,”

[解决办法]
protected void Button1_Click(object sender, EventArgs e)
{

if (WriteFile(this.TextBox1.Text.ToString(), this.TextBox2.Text.ToString(), this.TextBox3.Text.ToString()))
{
Page.RegisterStartupScript( "e ", " <script> alert( '添加成功 '); </script> ");

}
else
{
Page.RegisterStartupScript( "e ", " <script> alert( '生成出错! '); </script> ");
}


}

#region 生成静态页面
private static bool WriteFile(string strText, string strContent, string strAuthor)
{
string path = HttpContext.Current.Server.MapPath( "../htm/ ");
Encoding code = Encoding.GetEncoding( "gb2312 ");

// 读取模板文件
string temp = HttpContext.Current.Server.MapPath( "../htm/template.htm ");
StreamReader sr = null;
StreamWriter sw = null;
string str = " ";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}

string htmlfilename = DateTime.Now.ToString( "yyyyMMddHHmmss ") + ".html ";

// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str = str.Replace( "$title$ ", strText); //模板页中的ShowArticle
str = str.Replace( "$caption$ ", strText);
str = str.Replace( "$content$ ", strContent);
str = str.Replace( "$autor$ ", strAuthor);
// 写文件
try
{
sw = new StreamWriter(path + htmlfilename, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}


return true;

}
#endregion


---------------------模版

<!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$ </title>

</head>

<body>

<table width= "100% " border= "0 " bgcolor= "#339900 ">

<tr>

<td height= "34 " align= "center " bgcolor= "#FFFFFF "> <span class= "STYLE1 "> $caption$ </span> </td>

</tr>

<tr>

<td height= "42 " bgcolor= "#FFFFFF "> <br />

<br />

内容:$content$
<br />
作者: $autor$
</td>

</tr>

</table>

</body>

</html>


----------------------aspx
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Default.aspx.cs " Inherits= "_Default " %>

<!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 runat= "server ">
<title> 无标题页 </title>
</head>
<body>


<form id= "form1 " runat= "server ">

<div>

标题: <asp:TextBox ID= "TextBox1 " runat= "server " Width= "178px "> </asp:TextBox> <br />

<br />

内容:

<asp:TextBox ID= "TextBox2 " runat= "server " Height= "163px " Width= "533px "> </asp:TextBox>
<asp:TextBox ID= "TextBox3 " runat= "server "> </asp:TextBox> <br />

<br />

<asp:Button ID= "Button1 " runat= "server " OnClick= "Button1_Click " Text= "提交 " /> <br />
</div>

</form>

</body>



</html>

读书人网 >asp.net

热点推荐