• 生成静态网页的简单新闻发布


    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>

    天道酬勤,厚积薄发。 君子之行,静以修身,俭以养德。 非淡泊无以明志,非宁静无以致远。 如有恒,何须三更起,半夜眠;最怕莫,三天打鱼两天晒网,竹篮打水一场空。
  • 相关阅读:
    WeX5那些坑
    项目总结-微信公众平台Html5
    项目总结-APP中的HTML5
    夜幕团队成员的工资究竟几 K ?
    Docker竟然还能这么玩?商业级4G代理搭建实战!
    今天,大佬云集的夜幕团队正式成立了!
    InnoDB物理行中null值的存储的推断与验证
    探究InnoDB数据页内部行的存储方式
    DAO模式
    JDBC
  • 原文地址:https://www.cnblogs.com/houweidong/p/3439357.html
Copyright © 2020-2023  润新知