10

How can I highlight the text of a query in the gridview control?

John Mokien
  • 435
  • 4
  • 12
  • While the code obviously won't be the same, I wrote a quick article several years ago highlighting (no pun intended) the basic approach: http://classicasp.aspfaq.com/general/how-do-i-highlight-words-in-a-string.html – Aaron Bertrand Mar 03 '12 at 14:51
  • Since this can involve a bit of code to achieve, have a look at: - http://evonet.com.au/gridview-with-highlighted-search-results/ – skub Mar 03 '12 at 14:17
  • This explains how: http://forums.asp.net/t/1109807.aspx/1?Highlighting+result+in+GridView – Steve Wellens Mar 03 '12 at 14:16

3 Answers3

2

if you want do this client side please follow this steps:

add jQuery reference to your page.add a text input calles txt_Search.

and then use this script:

 $(document).ready(function () {
            $('#txt_Search').keyup(function () {
                searchTable($(this).val());
            });

            function searchTable(inputVal) {
                var table = $('#GridView1');
                table.find('tr').each(function (index, row) {
                    var allCells = $(row).find('td');
                    if (allCells.length > 0) {
                        var found = false;
                        allCells.each(
            function (index, td) {
                var regExp = new RegExp(inputVal, 'i');
                if (regExp.test($(td).text())) {
                    found = true;
                    return false;
                }});
                        if (found == true) $(row).show(); else $(row).hide();
                    }
                });
            }
        });
Arian
  • 12,793
  • 66
  • 176
  • 300
0
var gv = document.getElementById("#GridView1");

    for (var i = 0; i < gv.all.length; i++) {
var cellValue = grid.rows[i].cells[0].elements[0];
cellValuestyle.background = '#DD00DD';
}
Hisham
  • 455
  • 4
  • 16
-4

search the text, dress them with a tag like <label>, and don't forget to add highlight style for the labels.

gsharp
  • 27,557
  • 22
  • 88
  • 134
Artineer
  • 11
  • 1