1。模板temppage.htm
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>x_atitle</title>
<style type="text/css">
.x_css_a
{
text-decoration:none;
}
.x_css_a:hover
{
text-decoration:none;
color:red;
}
</style>
</head>
<body>
<div style="width:500px">
<div>id:x_aid</div><hr />
<div>标题:x_atitle</div><hr />
<div>内容:<hr />x_acontent</div>
<hr />
<div>x_SeparatePage</div>
</div>
</body>
</html>
2。CreatePage.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" AllowPaging="True"
onpageindexchanging="GridView1_PageIndexChanging" PageSize="5">
<Columns>
<asp:BoundField DataField="aid" HeaderText="aid">
<ItemStyle Width="60px" />
</asp:BoundField>
<asp:BoundField DataField="atitle" HeaderText="atitle" >
<ItemStyle Width="200px" />
</asp:BoundField>
<asp:BoundField DataField="acontent" HeaderText="acontent" >
<ItemStyle Width="700px" />
</asp:BoundField>
<asp:BoundField DataField="filename" HeaderText="filename" />
</Columns>
</asp:GridView>
</div>
<asp:Button Text="生成静态页面" runat="server" ID="Button1" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="删除所有静态页面" onclick="Button2_Click"
style="height: 26px" />
</form>
</body>
</html>
3。后台处理
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Init_GV();
}
}
/// <summary>
/// 初始化GV
/// </summary>
protected void Init_GV()
{
this.GridView1.DataSource = new ArticleDA().GetArticleDs();
this.GridView1.DataBind();
}
/// <summary>
/// 生成静态页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
DataSet ds = new ArticleDA().GetArticleDs();
int pageCount = ds.Tables[0].Rows.Count;//查询所有的记录数
for (int i = 0; i < pageCount; i++)
{
string tempPath = Server.MapPath("temppage.htm");//获得模板的物理路径
string strDate = DateTime.Now.ToString("yyyyMMddHHmmss");//生成文件名
string FilePath = Server.MapPath("CreateFy") + "/";//生成超链接的文件
string strContent = "";
StreamReader sr = null;
StreamWriter sw = null;
//读取模板文件
try
{
sr = new StreamReader(tempPath);
strContent = sr.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
sr.Close();
}
//替换模板中的内容
strContent = strContent.Replace("x_aid", ds.Tables[0].Rows[i]["aid"].ToString());
strContent = strContent.Replace("x_atitle", ds.Tables[0].Rows[i]["atitle"].ToString());
strContent = strContent.Replace("x_acontent", ds.Tables[0].Rows[i]["acontent"].ToString());
if (pageCount == 1) //如果只有一条记录时,不分页
{
strContent = strContent.Replace("x_SeparatePage", "");
}
else
{
strContent = strContent.Replace("x_SeparatePage", "<table align='center'><tr><td width='100px'>x_first</td><td width='100px'> x_uppage</td><td width='100px'> x_downpage</td><td width='100px'> x_end</td><tr></table>");
}
if (i == 0)//第一页时,首页和上一页不显示
{
//strContent = strContent.Replace("x_first", "");
//strContent = strContent.Replace("x_uppage", "");
strContent = strContent.Replace("x_first", "<a href='#' class='x_css_a'>首页</a>");
strContent = strContent.Replace("x_uppage", "<a href='#' class='x_css_a'>上一页</a>");
}
else
{
int ii = i - 1;
strContent = strContent.Replace("x_first", "<a href='" + strDate + "_" + "0.htm' class='x_css_a'>首页</a>");
strContent = strContent.Replace("x_uppage", "<a href='" + strDate + "_" + ii + ".htm' class='x_css_a'>上一页</a>");
}
if (i == pageCount - 1)//最后一页时,下一页和尾页不显示
{
//strContent = strContent.Replace("x_end", "");
//strContent = strContent.Replace("x_downpage", "");
strContent = strContent.Replace("x_downpage", "<a href='#' class='x_css_a'>下一页</a>");
strContent = strContent.Replace("x_end", "<a href='#' class='x_css_a'>尾页</a>");
}
else
{
int ii = i + 1;
strContent = strContent.Replace("x_downpage", "<a href='" + strDate + "_" + ii + ".htm' class='x_css_a'>下一页</a>");
strContent = strContent.Replace("x_end", "<a href='" + strDate + "_" + (pageCount - 1) + ".htm' class='x_css_a'>尾页</a>");
}
FilePath = FilePath + strDate + "_" + i + ".htm";//生成文件名及物理地址
//创建并写入静态页面
try
{
sw = new StreamWriter(FilePath, false, Encoding.GetEncoding("gb2312"));
sw.Write(strContent);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
sw.Flush();
sw.Close();
}
}
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('生成页面成功')</script>");
/*
* x_SeparatePage:x_first x_uppage x_downpage x_end
* x_atitle:标题
* x_acontent:内容
* x_first:<a href='#' class='x_css_a'>首页</a>
* x_uppage:<a href='#' class='x_css_a'>上一页</a>
* x_downpage:<a href='#' class='x_css_a'>下一页</a>
* x_end:<a href='#' class='x_css_a'>尾页</a>
*/
}
/// <summary>
/// 删除所有静态页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button2_Click(object sender, EventArgs e)
{
string FilePath = Server.MapPath("CreateFy");//根据文件名称,取得文件的物理路径
Response.Write(FilePath + "<hr/>");
Response.Write(Directory.Exists(FilePath) + "<hr/>"); //根据文件的物理路径,判断文件是否存在
if (Directory.Exists(FilePath))
{
Directory.Delete(FilePath, true);//删除并移除里面所有的文件
Response.Write("删除成功<hr/>");
Directory.CreateDirectory(FilePath);
Response.Write("重新创建成功<hr/>");
}
}
/// <summary>
/// GV分页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
Init_GV();
}
联系客服