public void Databind() { //要显示关键字(1),还是Tag(2) string keyOrTags = EyouSoft.Common.Utils.GetQueryStringValue("keyOrTags"); //关键字Tag标题 string keyOrTagsTitle = EyouSoft.Common.Utils.GetQueryStringValue("keyOrTagsTitle"); //是否要异步请求数据 bool isAjax = Convert.ToBoolean(EyouSoft.Common.Utils.GetQueryStringValue("isAjax")); EyouSoft.BLL.NewsStructure.TagKeyInfo tagKeyInfoBLL = new EyouSoft.BLL.NewsStructure.TagKeyInfo(); IList <EyouSoft.Model.NewsStructure.TagKeyInfo> list = new List <EyouSoft.Model.NewsStructure.TagKeyInfo>(); System.Text.StringBuilder str = new System.Text.StringBuilder(); //是关键字 if (keyOrTags == "1") { if (!string.IsNullOrEmpty(keyOrTagsTitle)) { list = tagKeyInfoBLL.GetAllKeyWord(keyOrTagsTitle); } else { list = tagKeyInfoBLL.GetAllKeyWord(); } } //Tag else if (keyOrTags == "2") { if (!string.IsNullOrEmpty(keyOrTagsTitle)) { list = tagKeyInfoBLL.GetAllTag(keyOrTagsTitle); } else { list = tagKeyInfoBLL.GetAllTag(); } } if (list != null) { for (int i = 0; i < list.Count; i++) { //每行显示8列,奇数列显示颜色:white,偶数列显示颜色:#f3f7ff if (i % 8 == 0) { str.AppendFormat("<tr bgcolor=\"{0}\">", ((i / 8)) % 2 == 0 ? "#f3f7ff" : "white"); } str.AppendFormat("<td><input id=\"{0}\" name=\"selKey\" type=\"checkbox\" /><label for=\"{0}\">{1}</label></td>", list[i].Id, list[i].ItemName); if (i + 1 % 10 == 0 && i > 0) { str.Append("</tr>"); } } } //是否异步请求 if (isAjax) { Response.Clear(); Response.Write(str); Response.End(); } list = null; tagKeyInfoBLL = null; Html = str.ToString(); }
/// <summary> /// 内容添加关键字 /// </summary> private void ContentAddKeys() { //计数器,一篇文章最多加5个关键字 int i = 0; //每篇文章加关键字的最大数量 int count = 5; //开始查找位置 int startIndex = 0; //每隔40个字符加一个关键字 int step = 40; //已处理过的关键字 System.Collections.Hashtable KeyEd = new System.Collections.Hashtable(); Hashtable replay = new Hashtable(); EyouSoft.BLL.NewsStructure.TagKeyInfo TagKeyInfoBll = new EyouSoft.BLL.NewsStructure.TagKeyInfo(); //得到所有的关键字 IList <EyouSoft.Model.NewsStructure.TagKeyInfo> list = TagKeyInfoBll.GetAllKeyWord(); //得到内容中的所有关键字 IList <EyouSoft.Model.NewsStructure.TagKeyInfo> AllKeyListInContent = new List <EyouSoft.Model.NewsStructure.TagKeyInfo>(); //用指定的字符串实例化StringBuilder StringBuilder strBuilder = null; //在加关键字之前先删除以前的关键字链接 string reg = ""; string NewsKeys = txtNewsKeys.Value.Trim() + ' ' + txtNewsKey.Value.Trim(); if (!string.IsNullOrEmpty(NewsKeys.Trim(' '))) { reg = NewsKeys.Trim(' ').Replace(' ', '|'); } if (!string.IsNullOrEmpty(reg)) { reg = reg.TrimEnd('|'); List <string> list2 = parseHref(Content, reg); if (list2 != null && list != null) { foreach (string str in list2) { foreach (EyouSoft.Model.NewsStructure.TagKeyInfo item in list) { if (str == item.ItemName) { AllKeyListInContent.Add(item); string attrReg = string.Format("(?<==\"[^=<>]*?){0}(?=[^=<>]*?\")", item.ItemName); string linkReg = string.Format("(?<=<a[\\s\\S]*?>[\\s\\S]*?){0}(?=[\\s\\S]*?</a>)", item.ItemName); string randStr = GenerateRandomString(item.ItemName.Length); while (replay.ContainsKey(randStr)) { randStr = GenerateRandomString(item.ItemName.Length); } if (!replay.ContainsValue(item.ItemName)) { Content = System.Text.RegularExpressions.Regex.Replace(Content, attrReg, randStr); Content = System.Text.RegularExpressions.Regex.Replace(Content, linkReg, randStr); replay.Add(randStr, item.ItemName); } break; } } } } } strBuilder = new StringBuilder(Content); //在加关键字之前先删除以前的关键字链接 if (list != null) { foreach (EyouSoft.Model.NewsStructure.TagKeyInfo item in list) { string temp = string.Empty; List <string> templist = parseHref(Content, string.Format("<a[^>]*?>{0}</a>", item.ItemName)); if (templist != null && templist.Count > 0) { temp = templist[0]; } if (!string.IsNullOrEmpty(temp)) { strBuilder = strBuilder.Replace(temp, item.ItemName); } } } int length = 0; if (AllKeyListInContent != null) { foreach (EyouSoft.Model.NewsStructure.TagKeyInfo item in AllKeyListInContent) { //如果替换大于等于最大次数,则跳出 if (i >= count || startIndex > strBuilder.Length) { break; } //开始替换的位置 int index = strBuilder.ToString().IndexOf(item.ItemName, startIndex); if (index == -1 && startIndex >= strBuilder.Length) { break; } ; if ((index - length) < step && length > 0) { //下次查找位置的开始 startIndex += item.ItemName.Length; continue; } if (index > -1) { //要替换成的字符串 string newString = string.Format("<a InnerLinkId=\"{0}\" {3} href=\"{1}\">{2}</a>", item.Id, item.ItemUrl == "" ? "javascript:" : item.ItemUrl, item.ItemName, string.IsNullOrEmpty(item.ItemUrl) ? "" : "target=\"blank\""); //替换的字符串 string oldString = item.ItemName; //判断是否存在相应的关键字 if (!KeyEd.ContainsKey(item.ItemName)) { //替换指定开始位置到指定长度内的所有字符串 strBuilder.Replace(oldString, newString, index, item.ItemName.Length); //将已处理的关键字存入哈希表,避免重复替换 KeyEd.Add(item.ItemName, item.Id); //计数器加1 i++; length = index + newString.Length; //下次查找位置的开始 startIndex = length; } else { //下次查找位置的开始 startIndex = index; } } } } System.Collections.IDictionaryEnumerator enumerator1 = replay.GetEnumerator(); while (enumerator1.MoveNext()) { string key = enumerator1.Key.ToString(); string valeu = enumerator1.Value.ToString(); strBuilder.Replace(key, valeu); } Content = strBuilder.ToString(); }