ASP.Net如何生成静态HTML页,带数据库连接的。

解决方案 »

  1.   

    http://www.google.cn/search?hl=zh-CN&q=ASP.Net+%E7%94%9F%E6%88%90%E9%9D%99%E6%80%81HTML%E9%A1%B5&btnG=Google+%E6%90%9C%E7%B4%A2&meta=
      

  2.   

    www.61-54.com是什么网站?上面也没有我想找的。
      

  3.   

    教程比较长,给你链接慢慢看
    http://www.yzcc.com/yzcc/aspj/14255518300.html
      

  4.   

    private void btnAdd_Click(object sender, System.EventArgs e)
    {
    string fullPath1=Server.MapPath("news");
    string fullPath2=Server.MapPath("news/");
    string fileName=DateTime.Now.ToString("yyyyMMddHHmmss")+".htm";
    string path1=fullPath1.Substring(fullPath1.LastIndexOf("\\")+1);
    string path2=fullPath1.Substring(fullPath1.LastIndexOf("\\")+1)+"/"+fileName;//写入数据库fPath
    string temp=Server.MapPath("news/news.htm");
    string title=this.txtTitle.Text.ToString();
    string class1=this.ddlClass.SelectedValue.ToString();
    string content=Convert.ToString(this.FreeTextBox1.Text.ToString());
    string  author=this.txtAuthor.Text.ToString();
    string from=this.txtFrom.Text.ToString();
    string mydate=System.DateTime.Now.ToString();
    string mydate2=System.DateTime.Now.ToShortDateString().ToString();
    int i=0;
    if(this.chkbImage.Checked)
    {
    i=1;
    }
    else
    {
    i=0;
    }
    int j=0;
    if(this.chkbImage.Checked)
    {
    j=1;
    }
    else
    {
    j=0;
    }
    int click=1;
    SqlConnection cn=DB.createCn();
    ////////////,'"+path2+"','"+fileName+"'静态页面添加
    string strSQL="insert into article (Title,Content,addTime,classID,writer,writeFrom,isImage,isHot,click,fPath,fName)"+
    "values ('"+title+"',@content,'"+mydate+"','"+class1+"','"+author+"','"+from+"','"+i+"','"+j+"','"+click+"','"+path2+"','"+fileName+"')";
    SqlCommand cmd=new SqlCommand(strSQL,cn);
    cmd.Parameters.Add(new SqlParameter("@content",SqlDbType.NText));
    cmd.Parameters["@content"].Value=this.FreeTextBox1.Text;
    SqlDataReader dr=cmd.ExecuteReader(); this.Panel1.Visible=false;
    Response.Write("添加新闻成功"); /////////////////////////////
    Encoding code=Encoding.GetEncoding("gb2312");
    StreamReader sr=null;
    StreamWriter sw=null;
    string str="";
    sr=new StreamReader(temp,code);
    str=sr.ReadToEnd();
    str=str.Replace("newstitle",this.txtTitle.Text.ToString());
    str=str.Replace("addTime",mydate2);
    str=str.Replace("from",from);
    str=str.Replace("htmlcontent",content);
    str=str.Replace("admin",author);
    sw=new StreamWriter(fullPath2+fileName,false,code);
    sw.Write(str);
    sw.Flush();
    /////////////////////////////
    Response.Write("<a href=addNews.aspx target=main>点击这里返回</a>");
    cn.Close();
      

  5.   

    其中news.htm是模版 代码
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <!-- saved from url=(0056)http://127.0.0.1/taknews/shownewsdetails.aspx?newsID=108 -->
    <HTML><HEAD><TITLE>新闻显示</TITLE>
    <META content="MSHTML 6.00.2900.2180" name=GENERATOR>
    <META content=C# name=CODE_LANGUAGE>
    <META content=JavaScript name=vs_defaultClientScript>
    <META content=http://schemas.microsoft.com/intellisense/ie5 
    name=vs_targetSchema>
    <STYLE type=text/css>BODY {
    MARGIN-TOP: 3px; FONT-WEIGHT: normal; FONT-SIZE: 12px; COLOR: #000000; FONT-STYLE: normal; FONT-FAMILY: "宋体"; BACKGROUND-COLOR: #d0dce0
    }
    TD {
    FONT-WEIGHT: normal; FONT-SIZE: 12px; COLOR: #000000; FONT-STYLE: normal; FONT-FAMILY: "宋体"
    }
    .maintitle {
    FONT-WEIGHT: bold; FONT-SIZE: 16px; COLOR: #ff3300; FONT-STYLE: italic; FONT-FAMILY: "仿宋_GB2312"
    }
    .subtitle {
    FONT-WEIGHT: bold; FONT-SIZE: 14px; COLOR: #cc00cc; FONT-FAMILY: "楷体_GB2312"
    }
    .green {
    COLOR: #006633
    }
    </STYLE><META http-equiv=Content-Type content="text/html; charset=utf-8"></HEAD>
    <BODY>
     <FONT face=宋体>
    <TABLE cellSpacing=0 cellPadding=0 width=777 align=center bgColor=#999999 
    border=0>
      <TBODY>
      <TR>
        <TD align=left>
          <TABLE cellSpacing=0 cellPadding=0 width="99%" align=left border=0>
            <TBODY>
            <TR>
              <TD vAlign=top align=left bgColor=#eeeeee height=349>
                <TABLE class=red cellSpacing=0 cellPadding=5 width="100%" 
                  border=0><TBODY>
                  <TR align=middle bgColor=#bbccff>
                    <TD height=32>
                      <DIV align=center><font face="宋体">title</font>
                        <P class=green align=center>加入时间:<SPAN id=lblAddTime 
                      style="COLOR: red"><font face="宋体"> addTime</font></SPAN>&nbsp;来源:<font face="宋体"> from</font> </P>
                      </DIV></TD>
                  </TR>
                  <TR>
                    <TD bgColor=#eeeeee>
                      <P class=green align=center>&nbsp;</P>
                      <P class=green align=center><BR></P>
                      <P></P></TD></TR></TBODY></TABLE>
                <TABLE cellSpacing=0 cellPadding=0 width="95%" align=center 
    border=0>
                  <TBODY>
                  <TR>
                    <TD><SPAN id=lblContent><font face="宋体">htmlcontent</font></SPAN></TD>
                  </TR></TBODY></TABLE>
                <TABLE height=18 cellSpacing=0 cellPadding=0 width="16%" align=right 
                border=0>
                  <TBODY>
                  <TR>
                    <TD><SPAN class=green><FONT face=宋体>(编辑:</FONT> <SPAN 
                      id=lblWriter>admin </SPAN>) </SPAN>
                      <DIV align=right></DIV></TD></TR></TBODY></TABLE><SPAN 
                class=green></SPAN></TD>
              <TD width=1 
    bgColor=#00007d></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></FONT></BODY></HTML>
      

  6.   

    如何生成静态页 
    方案1:
    /// <summary>
    /// 传入URL返回网页的html代码
    /// </summary>
    /// <param name="Url">URL</param>
    /// <returns></returns>
    public static  string getUrltoHtml(string Url)
    {
    errorMsg = "";
    try
    {
    System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
    // Get the response instance.
    System.Net.WebResponse wResp =wReq.GetResponse();
    // Read an HTTP-specific property
    //if (wResp.GetType() ==HttpWebResponse)
    //{
    //DateTime updated  =((System.Net.HttpWebResponse)wResp).LastModified;
    //}
    // Get the response stream.
    System.IO.Stream respStream  = wResp.GetResponseStream();
    // Dim reader As StreamReader = New StreamReader(respStream)
    System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
    return  reader.ReadToEnd();}
    catch(System.Exception ex)
    {
    errorMsg = ex.Message ;
    }
    return "";
    }你可以用这个函数获取网页的客户端的html代码,然后保存到.html文件里就可以了。方案2:
    生成单个的静态页面不是难点,难的是各个静态页面间的关联和链接如何保持完整;
    特别是在页面频繁更新、修改、或删除的情况下;像阿里巴巴的页面也全部是html的,估计用的是地址映射的功能
    关于地址映射可参考:http://www.easewe.com/Article/ShowArticle.aspx?article=131可以看看这个页面,分析一下他的“竞价倒计时”功能
    http://info.china.alibaba.com/news/subject/v1-s5011580.html?head=top4&Bidding=home5ASP.Net生成静态HTML页
    在Asp中实现的生成静态页用到的FileSystemObject对象!
    在.Net中涉及此类操作的是System.IO 
    以下是程序代码 注:此代码非原创!参考别人代码  CODE:   
    //生成HTML页
      public static bool WriteFile(string strText,string strContent,string strAuthor) 
      {
       string path = HttpContext.Current.Server.MapPath("/news/");
       Encoding code = Encoding.GetEncoding("gb2312");
       // 读取模板文件
       string temp = HttpContext.Current.Server.MapPath("/news/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=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
       // 替换内容
       // 这时,模板文件已经读入到名称为str的变量中了
       str =str.Replace("ShowArticle",strText); //模板页中的ShowArticle
       str = str.Replace("biaoti",strText);
       str = str.Replace("content",strContent);
       str = str.Replace("author",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;此函数放在Conn.CS基类中了
    在添加新闻的代码中引用 注:工程名为Hover    if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
        {
         Response.Write("添加成功");
        }
        else
        {
         Response.Write("生成HTML出错!");
        } 
     
    模板页Text.html代码
      CODE:   
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
      <title>ShowArticle</title>
       <body>
    biaoti
    <br>
    content<br>
    author
    </body>
    </HTML>
    biaoti
    <br>
    content<br>
    author
    </body>
    </HTML> 
     
    提示添加成功后会出以当前时间为文件名的html文件!上面只是把传递过来的几个参数直接写入了HTML文件中,在实际应用中需要先添加数据库,然后再写入HTML文件方案3:
    给一个客户端参考的例子(SJ)
    它的作用在于以客户端的方式获取某个页面的代码,然后可以做为其他用途,本例是直接输出
    <script>
        var oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        oXmlHttp.open("GET","http://www.163.com", false);
        oXmlHttp.send()
        var oStream = new ActiveXObject("ADODB.Stream");
        if(oStream == null)
            alert("您的机器不支持ADODB.Stream.")
        else
        {
            oStream.Type=1;
            oStream.Mode=3;
            oStream.Open() ;
            oStream.Write(oXmlHttp.responseBody);
            oStream.Position= 0;
            oStream.Type= 2;
            oStream.Charset="gb2312";
            var result= oStream.ReadText();
            oStream.Close();
            oStream = null;
      var aa = window.open("","")
      document.write(result);
            aa.document.write(result);
        }
    </script>方案4:学csdn一样。用xml保存数据,模版XSL也只有一个文件。
    使用xml来保存数据,使用xsl来定义模板并且生称数据。可以通过xsl来很方便的在客户端或者服务段显示数据。如果要生成静态叶面那更简单了。去查一下.net的xml类包问题解决。
                优点:可以方便快速转换成你想要的格式和内容。
                缺点:需要学习更多的内容,不好入门。方案5:
    思路   1. 利用如Dw-Mx这样的工具生成html格式的模板,在需要添加格式的地方加入特殊标记(如$htmlformat$),动态生成文件时利用代码读取此模板,然后获得前台输入的内容,添加到此模板的标记位置中,生成新文件名后写入磁盘,写入后再向数据库中写入相关数据。
           2. 使用后台代码硬编码Html文件,可以使用HtmlTextWriter类来写html文件。 
    优点   1. 可以建立非常复杂的页面,利用包含js文件的方法,在js文件内加入document.write()方法可以在所有页面内加入如页面头,广告等内容。   2. 静态html文件利用MS Windows2000的Index Server可以建立全文搜索引擎,利用asp.net可以以DataTable的方式得到搜索结果。而Win2000的Index服务无法查找xml文件的内容。如果包括了数据库搜索与Index索引双重查找,那么此搜索功能将非常强大。   3. 节省服务器的负荷,请求一个静态的html文件比一个aspx文件服务器资源节省许多。   缺点   思路二: 如果用硬编码的方式,工作量非常大,需要非常多的html代码。调试困难。而且使用硬编码生成的html样式无法修改,如果网站更换样式,那么必须得重新编码,给后期带来巨大的工作量。   因此这里采用的是第一种思路   示列代码   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%" border="0" 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代码:   //---------------------读html模板页面到stringbuilder对象里----   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>生成的模板html页面</marquee>";//文字说明   //----------替换htm里的标记为你想加的内容   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文件。程序使用了是循环替换,因此对需替换大量元素的模板速度非常快。