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;
}
}
}