Wednesday, September 3, 2008

Highlighting Multiple Search Keywords in ASP.NET

Highlight multiple key words no matter where they were in the text.

ASP.NET

By Dimitrios Markatos
Published: 12/1/2005

Introduction

A few years ago I had written a short article that demonstrated how one could highlight a keyword within a DataGrid control based on what was passed into it. Although this functionality served its purpose, it was however limited to one search word at a time; multiple search terms didn't apply. Since then I needed to find out a way to actually highlight multiple key words no matter where they were in the text.

As a result, I came up with a method, again using the ever handy regular expressions that allows the user the ability to highlight multiple keywords within any given body of text, including any web controls. In this example, however, the main player we'll be using is the MatchEvaluator Delegate method. Delegates are simply very useful function pointers that handle the operation using various approaches. Moreover, they do not contain nor define any real functional code, but rather assign the function that is to be used in its place. And this is what will allow us such functionality.

So before we get into the inner workings, I have listed the entire page code in both C# and VB, so readers have the ability to use their native language code without any unnecessary conversion on their part. So cut and paste the appropriate section of code and give it a spin.

http://dotnetjunkies.com/Tutorial/195E323C-78F3-4884-A5AA-3A1081AC3B35.dcik

ASP.Net Feeds