0

Possible Duplicate:
How can I highlight a word

I have a gridview and a textbox for searching text from a column. How can I highlight the result of the text in the column. I've searched and asked and I get hard things to code..would someone give me a simple code something ? thanks

Community
  • 1
  • 1
John Mokien
  • 435
  • 4
  • 12
  • You could opt for a jQuery method instead: http://stackoverflow.com/questions/119441/highlight-a-word-with-jquery – Dan Ellis Mar 03 '12 at 15:13

1 Answers1

1

I can give you an example.

Let say that the search box is:

txtSearchForMe.Text

Then you make the field on the Grid that show the data you won to highlight

<asp:TemplateField HeaderText="Text" >
    <ItemTemplate ><%#GetText(Container.DataItem)%></ItemTemplate>
</asp:TemplateField>

And the code behind

protected string GetText(object oItem)
{
    if(txtSearchForMe.Text.Lenght > 0)
    {
        return DataBinder.Eval(oItem, "cText").Replace(txtSearchForMe.Text, "<b>" + txtSearchForMe.Text + "</b>");
    }
    else
    {
        return DataBinder.Eval(oItem, "cText");
    }        
}

This is a simple idea, you can make it more complicate by splinting the search string to array of separated words and highlight them all.

Aristos
  • 66,005
  • 16
  • 114
  • 150