日期:2014-05-18  浏览次数:20558 次

这个效果怎么实现?将搜索结果中的关键字用红色字体显示?
我用的是Datagrid   ,我的搜索结果在某一列显示   ,我想把这一列中的关键字用红字表示,怎么做?

就是像百度搜索那样,先谢谢了!

------解决方案--------------------
如果只是一个搜索项的话,最简单的就是替换了Replace
------解决方案--------------------
replace( "关键字 ", " <color = red> 关键字 </color> ");
------解决方案--------------------
把关键字替换掉就可以了
.Replace( "keyword ", " <span style= 'color:red '> keyword </span> ");
------解决方案--------------------
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
{
string ToPlaceText = " ";
string TitleText = " ";
TitleText = ((HyperLink)e.Item.Cells[0].Controls[1]).Text.ToString();
ToPlaceText = TextBox1.Text;
TitleText = Regex.Replace(TitleText, Regex.Escape(ToPlaceText), " <font color=red> $& </font> ",RegexOptions.IgnoreCase);
((HyperLink)e.Item.Cells[0].Controls[1]).Text = TitleText;
}
}