arrow2 软件系统 arrow2 编程技术
一段用C#生成静态页面(包括分页)的测试代码
C#,生成静态
2007-10-08 15:49:46  

字体         收藏 推荐 打印 关闭

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using WaiMao.Class;
using System.IO;
using System.Text.RegularExpressions;

namespace WaiMao
{
        /// <summary>
        /// test1 的摘要说明。
        /// </summary>
        public class test1 : System.Web.UI.Page
        {
                protected System.Web.UI.WebControls.DataList orders;
                protected System.Web.UI.WebControls.Button Button1;
                private Class.EputianProducts ep=new EputianProducts();
               
                #region Web 窗体设计器生成的代码
                override protected void OnInit(EventArgs e)
                {
                        //
                        // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
                        //
                        InitializeComponent();
                        base.OnInit(e);
                }
               
                /// <summary>
                /// 设计器支持所需的方法 - 不要使用代码编辑器修改
                /// 此方法的内容。
                /// </summary>
                private void InitializeComponent()
                {   
                        this.Button1.Click += new System.EventHandler(this.Button1_Click);
                        this.Load += new System.EventHandler(this.Page_Load);

                }
                #endregion

                private void Page_Load(object sender, System.EventArgs e)
                {
                        if(!Page.IsPostBack)
                        {
                                //this.BindOrders();
                        }
                }
                private void BindOrders()
                {
                        string strSql="select * from orders";
                        DataTable dt=ep.GetDataTable(strSql);
                        this.orders.DataSource=dt;
                        this.orders.DataBind();
                }

                private void Button1_Click(object sender, System.EventArgs e)
                {
                        //读取top.html
                        StreamReader reader= new StreamReader( Server.MapPath("tpl/top.html"),System.Text.Encoding.GetEncoding("gb2312"));
                        string strTop="";
                        while(reader.Peek()!=-1)
                        {
                                strTop+=reader.ReadLine()+'\r'+'\n';
                        }

                       
                        //Page.Response.Write(s);
                        //读取body.html
                        reader=new StreamReader(Server.MapPath("tpl/body.html"),System.Text.Encoding.GetEncoding("gb2312"));
                        string strBody="";
                        while(reader.Peek()!=-1)
                        {
                                strBody+=reader.ReadLine()+'\r'+'\n';
                        }
                        //读取top.html
                        strBody=strBody.Replace("{top}",strTop);
                        //读取order循环标签
                        string strValue=this.getloop(strBody,"{loop-orders-begin}","{loop-orders-end}");
                        //去处标签
                        strBody=strBody.Replace("{loop-orders-begin}",string.Empty);
                        strBody=strBody.Replace("{loop-orders-end}",string.Empty);
                        //循环处理orders
                        string strOrders="";
                        string strSql="select * from orders";
                        DataTable dt=ep.GetDataTable(strSql);
                        int j=0;
                        string strTemp="";
                        for(int i=1;i<=dt.Rows.Count;i++)
                        {
                                strOrders+=strValue.Replace("{orders}",dt.Rows[i-1]["customerid"].ToString());
                                if(i%10==0)
                                {
                                        j++;
                                        strTemp=strBody.Replace(strValue,strOrders);
                                        //第一次生成分页页面
                                        this.WriteHtml(strTemp,Server.MapPath("..")+"/html/1_"+j.ToString()+".html");
                                       

                                }
                               
                        }
                        //写入分页代码                                       
                        for(int ii = 1;ii<=j;ii++)
                        {
                                this.WriteHtml(this.ReadHtml(Server.MapPath("..")+"/html/1_"+(ii).ToString()+".html").Replace("{pagebutton}","第:"+ii.ToString()),Server.MapPath("..")+"/html/1_"+(ii).ToString()+".html");
                        }
                       
                        reader.Close();
                        //strBody=strBody.Replace(strValue,strOrders);
                       

                        //Page.Response.Write(strBody);
                }       
                /// <summary>
                /// 获得标签内参数
                /// </summary>
                /// <param name="strN">目标字符串</param>
                /// <param name="strB">标签开始</param>
                /// <param name="strE">标签结束</param>
                /// <returns></returns>
                private string getloop(string strN,string strB,string strE)
                {
                        string strValue="";
                        //string strRegex ="{loop-orders-begin}"+"(?<value>[\\s\\S]*?)"+"{loop-orders-end}";
                        string strRegex = strB+"(?<value>[\\s\\S]*?)"+strE;
                        Regex re = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.Multiline);
                        foreach (Match m in re.Matches(strN))
                        {
                                strValue += m.Groups["value"].Value;
                        }                       
                        return strValue;
                }
                private void WriteHtml(string strBody,string FileName)
                {
                        try
                        {
                                System.IO.StreamWriter sw = new StreamWriter(FileName,false,System.Text.Encoding.GetEncoding("gb2312"));
                                sw.Write(strBody);
                                sw.Flush();
                                sw.Close();
                        }
                        catch(Exception ex)
                        {
                                HttpContext.Current.Response.Write(ex.Message);
                                HttpContext.Current.Response.End();
                        }
                }
                private string ReadHtml(string FileName)
                {
                        StreamReader reader=new StreamReader(FileName,System.Text.Encoding.GetEncoding("gb2312"));
                        string strBody="";
                        while(reader.Peek()!=-1)
                        {
                                strBody+=reader.ReadLine()+'\r'+'\n';
                        }
                        reader.Close();
                        return strBody;
                }
        }
}
来自:不详   作者: 未知   责任编辑:admin | 返回顶部


用户评论
文章分类


名称 作者 内容 论坛

关键字
C#,生成静态

google中搜索
相关文章
  · C# 打造个性 ...
  · C#遍历菜单并 ...
  · C#实现回车自 ...
  · 用C#轻松在DOT ...
  · C#的图形绘制 ...
  · C#制作图像旋 ...
  · 利用C#制作公 ...
  · C#生成中文汉 ...
  · C#下的实现在 ...
  · 在C#的WinForm ...
  · 用C# 编写完全 ...
  · Asp.NET 生成静 ...

最新发表
  ·  C# 打造个性浏览器
  ·  域名解析操作说明
  ·  W2K下安装MYSQL 4.0.21
  ·  Asp.net中执行Url重写
  ·  第三方Web Html Editor的使用
  ·  Iframe跨域问题.
  ·  垃圾邮件评判优化
  ·  如何对电子商务系统进行需求分析
  ·  IPS支付说明
  ·  谈谈PHP文章系统和SEO的关系