目前我只会:
1.生成静态首页的方法.
2.对新闻利用模板生成静态页面的方法.
请教:如何将有gridview和datalist的aspx页面生成静态页面?

解决方案 »

  1.   

    自己写table,其实gridview编译出来就是一个table
      

  2.   

    lovehongyun :能给个例子么??/
      

  3.   

    这个简单 教你个最简单的办法EnableViewState=false  //减少静态页的大小
    //urlstr是静态文件名 如:
    //urlstr="index.htm";
     protected override void Render(HtmlTextWriter writer)
            {
                if (!File.Exists(Server.MapPath(urlstr)))
                {
                    System.IO.StringWriter html = new System.IO.StringWriter();
                    System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(html);
                    base.Render(tw);
                    System.IO.StreamWriter sw;
                    sw = new System.IO.StreamWriter(Server.MapPath(urlstr), false, System.Text.Encoding.Default);
                    sw.Write(html.ToString());
                    sw.Close();
                    tw.Close();
                    //Server.Transfer(urlstr);
                    Response.Redirect(urlstr);
                }
                else
                {
                    Response.Redirect(urlstr);
                    //Server.Transfer(urlstr);
                }
            }
      

  4.   

    如果html页面存在,就直接跳转,不存在就生成html我目前的项目就这么做的
      

  5.   

    回: computer_li 首页的静态页面我已经是这样做的.
    现在我的内容页里有要做html.
    有没有别的办法?
      

  6.   

    首页的静态页面技术我已经知道了.现在我的问题是.如何将有gridview和datalist的aspx页面生成静态页面
      

  7.   

    而且Render已经是页面呈现的最后一个动作,你在此处再进行判断,显然不妥,性能会降低不少,正确应该是在HttpModule里的BeginRequest 事件里进行判断。
    具体做法可以参考:
    ==============================
    先定义一个StaticFileCacheModule
    实现IHttpModule接口
    定制BeginRequest事件  
    public class StaticFileCacheModule:IHttpModule
    {
       public void Init(HttpApplication context)
       {
         context.BeginRequest += new EventHandler(context_BeginRequest);
       }   private void context_BeginRequest(object sender, EventArgs e)
       {
        HttpContext context = ((HttpApplication)sender).Context;     //判断是否需要处理
       if (context.Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith(".aspx"))
          {            string fileUrl = "~/CacheFile/";            string fileName = GetFileName(context);            string filePath = context.Server.MapPath(fileUrl) + fileName;            if (File.Exists(filePath))            {                //如果静态缓存文件存在,直接返回缓存文件                context.RewritePath(fileUrl + fileName, false);            }        }    }    public static string GetFileName(HttpContext context)    {        //我们的缓存文件名由页面文件名加上查询字符串组成        return context.Request.AppRelativeCurrentExecutionFilePath.ToLower()            .Replace(".aspx", "").Replace("~/", "").Split('/')[context.Request.AppRelativeCurrentExecutionFilePath.ToLower()            .Replace(".aspx", "").Replace("~/", "").Split('/').Length-1]            + context.Request.Url.Query.Replace("?", "__").Replace("&", "_") + ".html";    }    public void Dispose() {}}再定义一个类
    要生成静态页面的继承它就行了    public abstract class static_htm : System.Web.UI.Page
        {
            
         protected override void Render(HtmlTextWriter writer)    {        StringWriter sw = new StringWriter();        HtmlTextWriter htmlw = new HtmlTextWriter(sw);        //调用Render方法,把页面内容输出到StringWriter中        base.Render(htmlw);        htmlw.Flush();        htmlw.Close();        //获得页面内容        string pageContent = sw.ToString();                    string path = Server.MapPath("~/CacheFile/");        if (!Directory.Exists(path))        {            Directory.CreateDirectory(path);        }         string pageUrl = StaticFileCacheModule.GetFileName(HttpContext.Current);        if(!File.Exists(pageUrl))        {   //把页面内容保存到静态文件中            using (StreamWriter stringWriter = File.CreateText(path + pageUrl))            {                stringWriter.Write(pageContent); ;            }        }
            //将页面内容输出到浏览器        Response.Write(pageContent);    }
    }当然不要忘在配置web.confing
      
        <httpModules>      <add name ="StaticFileCache" type="StaticFileCacheModule"/>    </httpModules>
      

  8.   

    一样的做法,不管你是否有gridview还是datagrid,最终在render方法呈现的都是html代码,所以用上面的静态页面生成技术生成的页面是没有任何问题的
      

  9.   

    请问:如何定义一个StaticFileCacheModule ?
    StaticFileCacheModule是一个类文件??
      

  10.   

    如果datalist要分页,又如何生成静态页面???
      

  11.   

    -_-! 我纳闷了,你们用网页保存html的方法来作,这个是不支持多线程的,你们是怎么批量生成??
      

  12.   

    运行后,另存为HTML格式,不就行了 ?
      

  13.   

    用ajax,将要读取的内容用ajax从一个gv页获取。
      

  14.   

    这种方法是不行的!因为只有第一页是htm页面如果有多页的话,第二页以后还是访问的aspx页面!!!不信你可以试试!!!
      

  15.   

    描述不清楚.
    是要把分页的全部页面生成静态吗?
    datalist页面生成的html代码可是点下一页的话要访问的还是aspx页面..而且文件名也要和生成的html页面名称相同.