分享

asp.net列表分页静态化

 wangn 2010-09-16
  //定义参数  
        StreamWriter sw = null;
        StreamReader sr = null;
        string htmlfilename = "" ;
        string str1 = "";
        int Count = 0;
        string path1 = HttpContext.Current.Server.MapPath("index.htm");
        string path=HttpContext.Current.Server.MapPath("~/");
        try
        {
            sr = new StreamReader(path1, Encoding.GetEncoding("GB2312"));
            str1 = sr.ReadToEnd();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            sr.Close();
        }
        //   数据库生成分页  
        int onepage = 2;
        string sSelect = "SELECT   count(*)   as   pcount   from   newsTable";
        DataClass dc = new DataClass();
        using (OleDbDataReader Dr = dc.bindDataReader(sSelect))
        {
            if (Dr.Read())
            {
                Count = int.Parse(Dr["pcount"].ToString());
            }
            dc.closeconn();
        }
        int allpages = Count / onepage;
        for (int i = 1; i <= allpages; i++)
        {
          string str = str1;
            htmlfilename = "index_" + "" + i + "" + ".html";
            int start = (i-1) * onepage;
            int nextpage = i + 1;
            int perpage = i - 1;
            if (i == 1)
            {
                start = 0;
                perpage =1;
            }
            if (perpage <= 0)
            {
                perpage = 1;
            }
            if (nextpage > allpages)
            {
                nextpage = i;
            }
            //   写文件  
            try
            {
                string vQuery = "SELECT   TOP   " + onepage + "   *   FROM   newsTable   WHERE (newsid   NOT   IN   (SELECT   TOP   " + start + "   newsid   FROM   newsTable   ORDER   BY   newsid)) ORDER   BY   newsid";

                if (i == 1)
                {
                    vQuery = "SELECT   TOP   " + onepage + "   *   FROM   newsTable   ORDER   BY   newsid";
                }

                DataClass dc1 = new DataClass();
                using (DataSet ds = dc1.bindsql(vQuery))
                {
                    DataTable dt = ds.Tables["zl"];
                sw = new StreamWriter(path + htmlfilename, false, Encoding.GetEncoding("GB2312"));
                string thbt = "";
                foreach (DataRow dr in dt.Rows)
                {
                    thbt += dr["newstitle"].ToString()+"<br>";
                }
                str=str.Replace("$news$",thbt);
                str=str.Replace("$fenye$","<a   href=" + "index_" + "" + perpage + "" + ".html" + ">上一页</a><a   href=" + "index_" + "" + nextpage + "" + ".html" + ">下一页</a>");
                sw.Write(str);
                sw.Flush();
                }
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
                HttpContext.Current.Response.End();
            }
            finally
            {
                sw.Close();
            }
        }

        Response.Write("<script>alert('生成成功');history.back();</script>");

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约