Implementing Search In Datalist and Highlight Search Text

 Let us see how to highlight the search keywords to make it easy for the user to identify his search results better. To perform this am using the Regex function to Highlight the search keywords.

This is the function used for highlighting ..

protected string HighlightText(string searchWord, string inputText)

{

if (searchWord != null && searchWord != "")

{

Regex expression = new Regex(searchWord.Replace(" ", "|"), RegexOptions.IgnoreCase);

return expression.Replace(inputText, new MatchEvaluator(ReplaceKeywords));

}

return inputText;

}

public string ReplaceKeywords(Match m)

{

return "" + m.Value + "";

}

To highlight the keyword u need to design your datalist lik this..

<asp:DataList ID="DataList1" runat="server">

<ItemTemplate>

<asp:Label ID="label1" runat="server"

Text='<%# Highlight(Eval("YourField").ToString()) %>'>

asp:Label>

ItemTemplate>

asp:DataList>

This ('<%# Highlight(Eval("YourField").ToString()) %>'>) is the one which highlights your Keyword. This is how we highlight the search keywords..

No Comments