2012-03-03 82 views
0

可能重複:
How can I highlight a word如何突出顯示gridview中文本的結果?

我有一個GridView並從一列搜索文本的文本框。 如何突出顯示列中文本的結果。 我已經搜索並問,我得到的代碼很難。有人會給我一個簡單的代碼的東西嗎?謝謝

+0

您可以選擇使用jQuery方法:http://stackoverflow.com/questions/119441/highlight-a-word-with-jquery – 2012-03-03 15:13:17

回答

1

我可以給你一個例子。

讓說,搜索框是:

txtSearchForMe.Text 

然後你就向您展示贏得了數據網格領域突出

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

背後

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"); 
    }   
} 
代碼

這是一個簡單的想法,可以通過將搜索字符串拼接到分隔數組中使其更加複雜單詞並突出顯示它們。