Asp.net 中高亮显示搜索关键字简单方法
2015-09-23 13:42
876 查看
今天用到搜索时的高亮显示,百度了一下,如下面:
1.替换关键字,对字体变色。
public static string ReplaceRed(string strtitle, string redkey)
{
if (redkey == "" || redkey == null)
{
return strtitle;
}
else
strtitle = strtitle.Replace(redkey, " <font color='#ff0000'>" + redkey + " </font>");
return strtitle;
}
2.用正则,CSS背景变色。
protected string HighlightText(string inputText,string searchWord)
{
System.Text.RegularExpressions.Regex expression = new System.Text.RegularExpressions.Regex(searchWord.Replace(" ", "|"), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return expression.Replace(inputText,new System.Text.RegularExpressions.MatchEvaluator(ReplaceKeywords));
}
public string ReplaceKeywords(System.Text.RegularExpressions.Match m)
{
return "<span class='highlightTxtSearch'>" + m.Value + "</span>";
}
CSS:
.highlightTxtSearch
{
background-color:Yellow;
}
上面的方法固然用着不错,但感觉过于繁琐。于是试着用下面的方法:
//声明关键字数组
string[] m_Key = new string[] { };
//搜索关键字不空,就执行替换
if (!string.IsNullOrEmpty(Request.QueryString["SearchKey"]))//或者 if (!string.IsNullOrEmpty(txtSearchKey.Text.Trim()))
{
//搜索关键字以空格隔开
m_Key = Request.QueryString["SearchKey"].Split(new char[] { ' ' });
for (int i = 0; i < m_Key.Length; i++)
{
lblOptimumRoad.Text = modelRoadSearch.OptimumRoad.Replace(m_Key[i], "<span class='highlightTxtSearch'>" + m_Key[i] + "</span>");
//如果在gridview中,可以在RowDataBound事件中进行判断,然后e.Row.Cells[3].Text = e.Row.Cells[3].Text.Replace(m_SearchKey[i], "<span class='highlightTxtSearch'>"+m_SearchKey[i]+"</span>");
}
}
else
{
//搜索关键字为空,则是从其它页面转来,不用高亮关键字
lblOptimumRoad.Text = modelRoadSearch.OptimumRoad;
}
不管这种方法效率如何,但感觉用着挺方便的。
1.替换关键字,对字体变色。
public static string ReplaceRed(string strtitle, string redkey)
{
if (redkey == "" || redkey == null)
{
return strtitle;
}
else
strtitle = strtitle.Replace(redkey, " <font color='#ff0000'>" + redkey + " </font>");
return strtitle;
}
2.用正则,CSS背景变色。
protected string HighlightText(string inputText,string searchWord)
{
System.Text.RegularExpressions.Regex expression = new System.Text.RegularExpressions.Regex(searchWord.Replace(" ", "|"), System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return expression.Replace(inputText,new System.Text.RegularExpressions.MatchEvaluator(ReplaceKeywords));
}
public string ReplaceKeywords(System.Text.RegularExpressions.Match m)
{
return "<span class='highlightTxtSearch'>" + m.Value + "</span>";
}
CSS:
.highlightTxtSearch
{
background-color:Yellow;
}
上面的方法固然用着不错,但感觉过于繁琐。于是试着用下面的方法:
//声明关键字数组
string[] m_Key = new string[] { };
//搜索关键字不空,就执行替换
if (!string.IsNullOrEmpty(Request.QueryString["SearchKey"]))//或者 if (!string.IsNullOrEmpty(txtSearchKey.Text.Trim()))
{
//搜索关键字以空格隔开
m_Key = Request.QueryString["SearchKey"].Split(new char[] { ' ' });
for (int i = 0; i < m_Key.Length; i++)
{
lblOptimumRoad.Text = modelRoadSearch.OptimumRoad.Replace(m_Key[i], "<span class='highlightTxtSearch'>" + m_Key[i] + "</span>");
//如果在gridview中,可以在RowDataBound事件中进行判断,然后e.Row.Cells[3].Text = e.Row.Cells[3].Text.Replace(m_SearchKey[i], "<span class='highlightTxtSearch'>"+m_SearchKey[i]+"</span>");
}
}
else
{
//搜索关键字为空,则是从其它页面转来,不用高亮关键字
lblOptimumRoad.Text = modelRoadSearch.OptimumRoad;
}
不管这种方法效率如何,但感觉用着挺方便的。
相关文章推荐
- asp.net快速连接access
- 如何对ASP.NET网站实现静态化
- 四步完成asp网页设计流量统计
- 简单的ASP统计制作实例
- asp.net中runat="server"的含义
- asp.net中runat="server"的含义
- ASP.NET 控件不用Disabled实现ReadOnly的效果,即字体不变灰色。
- [ASP.NET]分析MVC5源码,并实现一个ASP.MVC
- 我的ASPxGridView控件的数据是在后台绑定的,但是实现不了筛选和分页的功能,只能显示一页的数据?
- ASP.NET MVC Razor 输出没有编码的HTML字符串
- 如何对ASP.NET网站实现静态化
- asp.net快速连接access
- asp.net判断session过期
- asp.net mvc Ajax.BeginForm不能异步刷新,或转到新页面,或页面还是刷新了,的原因(或解决办法)
- ASP.NET MVC3 Model验证总结
- ASP.NET用户控件如何使用
- ASP.NET网站导航及导航控件如何使用
- ASP.NET(C#)读取Excel
- ASP.NET FileUpload读取上传文件的内容
- ASP.NET本质论阅读----线程与异步