Search results contained in templates are not highlighted. This example demonstrates how to highlight them manually.
In this example, DataItemTemplate contains the ASPxLabel control.
<dx:GridViewDataTextColumn FieldName="CategoryName" >
<DataItemTemplate>
<dx:ASPxImage ID="ASPxImage1" runat="server" ShowLoadingImage="true" Width="50" ImageUrl='<%# string.Format("~/Images/{0}.jpg", Eval("CategoryID")) %>' />
<dx:ASPxLabel ID="label" runat="server" Text='<%# Eval("CategoryName") %>' EncodeHtml="false" OnDataBound="label_DataBound" />
</DataItemTemplate>
</dx:GridViewDataTextColumn>
The ASPxLabel.DataBound
event handler looks for the search text and applies the dxgvHL
CSS class to the found text.
protected void label_DataBound(object sender, EventArgs e) {
ASPxLabel label = sender as ASPxLabel;
label.Text = HighlightSearchText(label.Text, gridView.SearchPanelFilter);
}
public static string HighlightSearchText(string source, string searchText) {
if (string.IsNullOrWhiteSpace(searchText))
return source;
var regex = new Regex(Regex.Escape(searchText), RegexOptions.IgnoreCase);
if (regex.IsMatch(source))
return string.Format("<span>{0}</span>", regex.Replace(source, "<span class='dxgvHL'>$0</span>"));
return source;
}
Note
This example illustrates how to process simple requests. If you create a composite criterion, it is necessary to perform additional operations to parse the search text.
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)