简体   繁体   中英

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 ! Thanks.

you will need to wrap basic html tag around your required word for doing that.

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
  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. 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. Grab it using jquery (or javascript) and process the 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..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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