[英]I want to highlight a specific word inside a gridview in asp.net C#
我有一个GridView,当我在文本之间导航时,我想要突出显示特定单词的很多文本,我想强调它总是我的意思是不是在搜索之后或者其他东西,让我们假设我在里面有以下文本gridview“我早上会有一个苹果,晚上会有一个苹果”我想通过文本突出显示gridview中的“apple”这个词,我尝试了一个java脚本但没有发生任何事! 谢谢。
你需要在你需要的单词周围包装基本的html标签。
所以改变你的字符串
"I would have an apple in the morning.."
至
"I would have an <span style='background-color:Yellow;'>apple </span>in the morning..".
一切都会开始奏效。
现在你可以在任何地方做到。
通过javascript / jquery,你可以这样做:a。 Gridview被渲染为纯html中的表,因此无论您提供给gridview的Id是什么,都将是表的Id。 使用jquery(或javascript)抓取它并处理innerHtml。
即
$(document).ready(function(){
var text= $('#GridView_Equivalent_Id').html();
//var text= $(#+'<%= GridView1.ClientID %>').html();
text= text.replace('apple','<span style="background-color:Yellow" >apple</span>');
$('#GridView_Equivalent_Id').html(text);
});
在服务器端,你可以这样做。
假设你有一个数据表,你绑定你的GridView和这个数据表的第二列有该字符串(句子),其中一个词,你想要突出显示..
做这个。
DataTable dt = GetDatafromDb();
foreach(DataRow row in dt.Rows)
{
row['columnContainingText'] = row['columnContainingText'].ToString().Replace("apple","<span style='background-color:yellow'>apple</span>");
}
GridView1.DataSource=dt;
GridView1.DataBind();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.