简体   繁体   English

我想在asp.net C#中的gridview中突出显示一个特定的单词

[英]I want to highlight a specific word inside a gridview in asp.net C#

I have a GridView that showing a lot of texts I want to highlight a specific word while I navigates between the text, I want to highlight it always I mean not from after a search or something, let's assume that I have the following text inside the gridview"I would have an apple in the morning, an apple in the evening" I want as I go through the text to highlight the word "apple" inside the gridview, I tried a java script but nothing happened ! 我有一个GridView,当我在文本之间导航时,我想要突出显示特定单词的很多文本,我想强调它总是我的意思是不是在搜索之后或者其他东西,让我们假设我在里面有以下文本gridview“我早上会有一个苹果,晚上会有一个苹果”我想通过文本突出显示gridview中的“apple”这个词,我尝试了一个java脚本但没有发生任何事! Thanks. 谢谢。

you will need to wrap basic html tag around your required word for doing that. 你需要在你需要的单词周围包装基本的html标签。

so change your string 所以改变你的字符串

"I would have an apple in the morning.."

to

"I would have an <span style='background-color:Yellow;'>apple </span>in the morning..".

and everything will start to work. 一切都会开始奏效。

Now you can do it anywhere. 现在你可以在任何地方做到。

  1. through javascript 通过JavaScript
  2. through server-side ie pre-processing your datasource for such strings. 通过服务器端即为这些字符串预处理数据源。 ie modify your datasource content accordingly in the code. 即在代码中相应地修改数据源内容。

Through javascript/jquery, you can do this: a. 通过javascript / jquery,你可以这样做:a。 Gridview gets rendered to a table in pure html, so whatever Id you have provided to the gridview, will be the Id of the table. Gridview被渲染为纯html中的表,因此无论您提供给gridview的Id是什么,都将是表的Id。 Grab it using jquery (or javascript) and process the innerHtml. 使用jquery(或javascript)抓取它并处理innerHtml。

ie

 $(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);
});

on the server side, you can do this. 在服务器端,你可以这样做。

Suppose you have a datatable to which you bind your GridView and 2nd Column of this datatable has that string(sentence), one of whose words, you want to highlight.. 假设你有一个数据表,你绑定你的GridView和这个数据表的第二列有该字符串(句子),其中一个词,你想要突出显示..

do this. 做这个。

   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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM