/// <summary> /// 解释并替换标签内容 /// </summary> /// <param name="temContent">模板页所有内容</param> /// <param name="flag">标记此标签的起止的索引的字符串。</param> public void ExplainSelf(object obj) { String[] sArray = (String[])obj; String savePath = sArray[0]; String flag = sArray[1]; //读取模板内容 String temContent = TemplateHandle.ReadTemplate(savePath); if (!temContent.Contains("{wlsd:pagelist")) { throw new TagErrorException("没有与{wlsd:list}相对应的{wlsd:pagelist}。"); } //分解起止索引 int[] flagArray = Array.ConvertAll<String, int>(flag.Split('$'), delegate(string s) { return int.Parse(s); }); if (flagArray.Length != 4) { throw new TagErrorException("没有{/wlsd:list}结束标记。"); } //解析标签内容 AnalyzeTag(temContent, flagArray); //读取此类别的所有数据,保存到DataTable。 //存储构造的sql语句 StringBuilder sqlsb = new StringBuilder(); sqlsb.Append("SELECT ROW_NUMBER() over(order by "); sqlsb.Append(this.OrderBy); sqlsb.Append(") as sort,* FROM Article "); sqlsb.Append(" WHERE 1=1 "); if (this.Category != String.Empty) { sqlsb.Append("AND CategoryID = "); sqlsb.Append(this.Category); sqlsb.Append(" order by "); sqlsb.Append(this.OrderBy); sqlsb.Append(" "); sqlsb.Append(this.orderway); } else { throw new TagErrorException("没有设置文章的类别属性:category。"); } SqlHelper sh = new SqlHelper(); DataTable dtArtc = sh.ExcuteDataTable(sqlsb.ToString()); //页数 int pagenum = (dtArtc.Rows.Count % this.PageSize) == 0 ? (dtArtc.Rows.Count / this.PageSize) : ((dtArtc.Rows.Count / this.PageSize) + 1); for (int i = 1; i <= pagenum; i++) { DataView dv = new DataView(dtArtc); dv.RowFilter = "sort > " + (i-1) * this.PageSize + " AND sort <= " + i * this.PageSize; //再读取一遍模板内容。 temContent = TemplateHandle.ReadTemplate(savePath); //组装html,并返回 String newStr = AssembleSelf(temContent, flagArray, dv.ToTable()); //用组装的新字符串去替换标签。 if (flagArray.Length == 4) { temContent = temContent.Remove(flagArray[0], flagArray[3] - flagArray[0]).Insert(flagArray[0], newStr); } else if (flagArray.Length == 2) { temContent = temContent.Remove(flagArray[0], flagArray[1] - flagArray[0]).Insert(flagArray[0], newStr); } String listPath = PathHelper.GetPathWithoutName(savePath) + this.Category + "\\" + i + ".htm"; if (!File.Exists(listPath)) { File.Create(listPath).Close(); } //先把模板复制到最终静态页要保存的地方。 File.Copy(savePath, listPath, true); //保存模板 TemplateHandle.SaveTemplate(listPath, temContent); } }