using System.Data.OleDb;
using System.IO;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=|datadirectory|news.mdb");
conn.Open();
string newsfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";
OleDbCommand comm = new OleDbCommand("insert into news(newstitle,newsfilename) values('" + TextBox1.Text + "','" + newsfilename + "')", conn);
if (WriteFile(this.TextBox1.Text, this.TextBox2.Text, newsfilename))
{
comm.ExecuteNonQuery();
}
else
{
Response.Write("error");
}
conn.Close();
}
public bool WriteFile(string ArticleTitle, string ArticleContent, string filename)
{
string OutPutPath = HttpContext.Current.Server.MapPath("p/");
Encoding encoding = Encoding.GetEncoding("gb2312");
// 读取模板文件
string ModelTemp = HttpContext.Current.Server.MapPath("ModelHTML.htm");
StreamReader sr = null;
StreamWriter sw = null;
string str = "";
try
{
sr = new StreamReader(ModelTemp, encoding);
str = sr.ReadToEnd(); // 读取文件
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
// 替换内容
str = str.Replace("PageTitle", ArticleTitle); //模板页中的PageArticle
str = str.Replace("ArticleTitle", ArticleTitle);
str = str.Replace("ArticleContent", ArticleContent);
// 写文件
try
{
sw = new StreamWriter(OutPutPath + filename, false, encoding);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
}
}
----------------------------------------------------------------------------------------------------
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="AccessDataSource1">
<Columns>
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:TemplateField HeaderText="newsfilename" SortExpression="newsfilename">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("newsfilename") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<a href='p/<%#Eval("newsfilename") %>' target="_blank"><%#Eval("newstitle") %></a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/news.mdb"
DeleteCommand="DELETE FROM [news] WHERE [id] = ?"
InsertCommand="INSERT INTO [news] ([id], [newstitle], [newscontent], [newsfilename]) VALUES (?, ?, ?, ?)"
SelectCommand="SELECT * FROM [news]"
UpdateCommand="UPDATE [news] SET [newstitle] = ?, [newscontent] = ?, [newsfilename] = ? WHERE [id] = ?">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="newstitle" Type="String" />
<asp:Parameter Name="newscontent" Type="String" />
<asp:Parameter Name="newsfilename" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="newstitle" Type="String" />
<asp:Parameter Name="newscontent" Type="String" />
<asp:Parameter Name="newsfilename" Type="String" />
</InsertParameters>
</asp:AccessDataSource>
</div>
</form>
</body>