文件名称:WebSites 带分页静态生成
文件大小:1.18MB
文件格式:RAR
更新时间:2012-08-10 02:18:24
动态生成静态
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
public partial class CreatePage : System.Web.UI.Page
{
Encoding gb = Encoding.GetEncoding("gb2312");
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = LoadTemplate("PageTemplate.txt");
string ls1 = LoadNewsList(1);
string ls2 = LoadNewsList(2);
string ls3 = LoadNewsList(3);
string pl1 = LoadPhotoList(1);
string pl2 = LoadPhotoList(2);
str=str.Replace("[--@title--]", "首页");
str=str.Replace("[--@NL1--]", ls1);
str=str.Replace("[--@NL2--]", ls2);
str=str.Replace("[--@NL3--]", ls3);
str = str.Replace("[--@PL1--]", pl1);
str = str.Replace("[--@PL2--]", pl2);
SavePage("index.html", str);
}
public string LoadPhotoList(int CategoryID)
{
string temp = LoadTemplate("PhotoList.txt");
string sql = "select top 4 ProductID,ProductName from Products where CategoryID=" + CategoryID + " order by ProductID desc";
SqlDataReader dr = ConnDB.ExecuteReader(sql);
string st="";
while (dr.Read())
{
string str = "";
str=temp.Replace("[--@filename--]",dr[0].ToString()+".jpg");
str=str.Replace("[--@ID--]",dr[0].ToString());
str=str.Replace("[--@photoname--]",dr[1].ToString());
st += str;
}
return st;
}
public string LoadNewsList(int CategoryID)
{
string sql = "select top 8 ProductID,ProductName from Products where CategoryID=" + CategoryID + " order by ProductID desc";
SqlDataReader dr = ConnDB.ExecuteReader(sql);
string temp = "";
while (dr.Read())
{
temp += "" + dr[1].ToString() + "
";
}
return temp;
}
public string LoadTemplate(string tempname)
{
return File.ReadAllText(MapPath(tempname),gb);
}
public void SavePage(string filename, string data)
{
byte[] buffer = gb.GetBytes(data);
FileStream fs = File.OpenWrite(MapPath(filename));
fs.Write(buffer, 0, buffer.Length);
fs.Flush();
fs.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
string sql = "select * from Products";
SqlDataReader dr = ConnDB.ExecuteReader(sql);
while (dr.Read())
{
string temp = LoadTemplate("NewsDetail.txt");
temp = temp.Replace("[--@title--]", dr["ProductName"].ToString());
temp = temp.Replace("[--@categoryname--]", dr["CategoryID"].ToString());
temp = temp.Replace("[--@author--]", dr["SupplierID"].ToString());
temp = temp.Replace("[--@postdate--]", DateTime.Now.ToLongDateString());
temp = temp.Replace("[--@content--]", dr["quantityperunit"].ToString());
string fn="news"+dr["ProductID"].ToString()+".html";
SavePage(fn,temp);
}
}
protected void Button4_Click(object sender, EventArgs e)
{
int PageSize = 20;
object obj = ConnDB.ExecuteScalar("exec paging 0,0,1");
double PageCount= Convert.ToInt32(Math.Ceiling(Convert.ToDouble(obj) / PageSize));
for (int i = 1; i <= PageCount; i++)
{
string temp = LoadTemplate("NewsList.txt");
string str = "";
string sql = "exec paging " + (i-1) * PageSize + "," +i* PageSize + ",0";
SqlDataReader dr = ConnDB.ExecuteReader(sql);
string str1 = "";
while (dr.Read())
{
str1 += "" + dr[1].ToString() + "
";
}
#region 生成页码
for (int j = 1; j <= PageCount; j++)
{
if (i == j)
{
str+= j;
}
else
{
str += ""+j+"";
}
}
#endregion
temp = temp.Replace("[--@newslist--]", str1);
temp =temp.Replace("[--@pager--]", str);
SavePage("NewsList" + i + ".html", temp);
}
}
}