Asp.Net静态带分页,
|
Asp.Net静态带分页, 之前一直想用控件来分页的,后来感觉还是试试SqlDataReader 的分页 在Junval's CSDN博客 认真看一下Sethtml()方法就知道生成静态是怎么回事了,莫非就是先读模版页,然后把模版页的内容提出来,把一些字符串进行替换,我这里是用了$htmlkey[1],$htmlkey[2],作为替换的项,再生成一个指定的文件,把刚才读出来的文本流写到新建的文件里去. 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.IO; using System.Text; using System.Data.SqlClient; public partial class fileAction_CtHtml : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Databind(); } public void Databind() { //这里链接数据库的,自己写吧 using (SqlConnection conn = DBconn.DB.conn()) { try {
string List = ""; string Pjoen=""; conn.Open(); int RecordCount = 0; //总共记录数 int PageCount = 0; //总共页数 int CurrentPage = 0; //当前页码 int PageSize = 7; //每页显示的条数 string Sql; //定义SQl RecordCount = SQLCount();//取得总的记录数 SQLCount()方法在下面. PageCount = RecordCount / PageSize;//获取页数 if (RecordCount % PageSize != 0)//如果不整除 PageCount = PageCount + 1;//如果不整除再加 for (int j = 1; j < PageCount + 1; j++)//循环列表的页数 { if (Request.QueryString["Page"] != null) CurrentPage = Convert.ToInt32(Request.QueryString["Page"]); else CurrentPage = j;
Sql = "Select Top " + PageSize + " * From News_content where ID Not In ("; Sql += "Select Top " + ((CurrentPage - 1) * PageSize) + " ID From News_content)"; Response.Write("Sql:" + Sql + "<br>"); //Response.End(); int i = 1; SqlCommand cmd = new SqlCommand(Sql, conn); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { List = List + i +".<a href =" + dr["id"].ToString() + ".sHtml> " + dr["title"].ToString() + "</a><br>"; i = i + 1; Sethtml(dr["Content"].ToString(), dr["id"].ToString(), "template");//调用生成静态页的函数.这里是生成单页内容的 } dr.Close(); int PagePre, PageNext; PagePre = CurrentPage - 1; PageNext = CurrentPage + 1; string PageHtml=""; if (PagePre == 0) PageHtml = "首页 上页"; else PageHtml += "<a href = 'News_1.sHtml'>首页</a>  <a href = 'News_" + PagePre + ".sHtml'>上页</a>"; if (PageNext > PageCount) PageHtml += " 下页 末页"; else PageHtml += " <a href='News_" + PageNext + ".sHtml'>下页</a> <a href='News_" + PageCount + ".sHtml'>尾页</a>"; List = List + "<br>" + PageHtml; //把字符串连接起来. Sethtml(List, "News_"+j, "template");//调用生成静态的方法,生成新闻列表加分页. List = ""; } mydata.InnerHtml = List;//在CtHtml.aspx的mydata里显示; } catch(Exception e) { throw new Exception(e.ToString()); } } } protected void Sethtml(string NewsTitle, string path, string template) //创建静态文件的方法 { string[] newContent = new string[2];//定义和HTML标记数目一致的数组
StringBuilder strhtml = new StringBuilder(); try { //创建StreamReader对象 using (StreamReader sr = new StreamReader(Server.MapPath("createHTML") + "/" + template + ".html")) { stringoneline; //读取指定的HTML文件模版 while ((oneline = sr.ReadLine()) != null) { strhtml.Append(oneline); } } } catch (Exception err) { Response.Write(err.ToString()); } newContent[0] = "<font color='red'>新闻标题</font>"; newContent[1] = NewsTitle; try {
string md = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString(); //取得时间 string HtmlFile = HttpContext.Current.Server.MapPath("createHtml/" + md); //获取路径 if (!System.IO.Directory.Exists(HtmlFile)) //是否存在 { System.IO.Directory.CreateDirectory(HtmlFile); //不存在就创建以日期为名的文件夹 } //指定要生成的HTML文件 string fname = Server.MapPath("createHtml/") + md + "/" + path + ".sHtml"; //替换HTML模版文件里的标记为新的内容 for (int i = 0; i < 2; i++) { strhtml.Replace("$htmlkey[" + i + "]", newContent[i]); } //创建文件信息对象 FileInfo finfo = new FileInfo(fname); //以打开或者写入的形式创建文件流 using (FileStream fs = finfo.OpenWrite()) { //根据上面创建的文件流创建写数据流 StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312")); //把新的内容写到创建的HTML页面中 sw.WriteLine(strhtml); sw.Flush(); sw.Close(); } } catch (Exception err) { Response.Write(err.ToString()); } } public int SQLCount() //返回数据库总计录数 { int RCount = 0; using (SqlConnection conn = DBconn.DB.conn()) { conn.Open(); string Sql = "Select Count(*) From News_Content"; SqlCommand cmd = new SqlCommand(Sql, conn); SqlDataReader MyRd = cmd.ExecuteReader(); if (MyRd.Read()) RCount =int.Parse(MyRd[0].ToString()); MyRd.Close(); return RCount; } } } Cthtml.aspx的代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CtHtml.aspx.cs" Inherits="fileAction_CtHtml" Debug="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd <html xmlns="http://www.w3.org/1999/xhtml <head runat="server"> <title>无标题页</title> <link href="createHTML/css.css" rel="stylesheet" type="text/css" /> </head> <body> <form id="form1" runat="server"> <div>
<span id="mydata" runat="server"></span> </div> </form> </body> </html> 模板页template.html的代码: <html xmlns="http://www.w3.org/1999/xhtml <head> <title>生成静态模版</title> <link href="../css.css" rel="stylesheet" type="text/css"> </head> <body> <table width="812" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="600"> $htmlkey[0] </td> </tr> <tr> <td height="166" valign="top"> $htmlkey[1] </td> </tr> </table> </body> </html> |