在网格视图中突出显示多个单词

时间:2013-06-29 05:43:37

标签: c# asp.net gridview

我需要突出显示用户在gridview中搜索它的所有单词

我试试这个

public string Highlight(string InputTxt)
{

    string Outputtext = "";
    Regex RegExp ;
    string[] separators = { ",", ".", "!", "?", ";", ":", " " };
    string[] words = InputTxt.Split(separators, StringSplitOptions.RemoveEmptyEntries);

    string strSearch = TextBox1.Text;
    string[] Strseacharr = strSearch.Split(separators, StringSplitOptions.RemoveEmptyEntries);

    foreach (var word in words)
    { 
        foreach(var wordtosearch in Strseacharr)
        {
            if (VSM.stem(word) == VSM.stem(wordtosearch))
            {
                RegExp =new Regex(word.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
                return Outputtext+=RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords));
            }             
        }
    }

    Label2.Text = Outputtext;
    return Outputtext+="";
}

public string ReplaceKeyWords(Match m)
{

    if (VSM.stem(m.Value.ToString()) == VSM.stem(TextBox1.Text))
        return "<span class=highlight>" +m.Value+ "</span>";
    else
        return m.Value;
    }

在我的gridview字段中我正在使用它

<asp:Label ID="Label6" runat="server" 
           Text='<%# Highlight(Eval("DocumentSummary").ToString()) %>'>
</asp:Label>

2 个答案:

答案 0 :(得分:0)

public string Highlight(string InputTxt)
    {
        string[] separators = { ",", ".", "!", "?", ";", ":", " " };
        string[] words = InputTxt.Split(separators, StringSplitOptions.RemoveEmptyEntries);

        string strSearch = TextBox1.Text;
        string[] Strseacharr = strSearch.Split(separators, StringSplitOptions.RemoveEmptyEntries);

        foreach (var item in Strseacharr)
        {
            InputTxt = InputTxt.Replace(item, ReplaceKeyWords(item));
        }
        return InputTxt;
    }

    public string ReplaceKeyWords(string m)
    {

        return "<span class=highlight>" + m + "</span>";
    }

这里是您的要求的完整代码。

答案 1 :(得分:0)

错误在于ReplaceKeyWords 不要......如果......其他的

public string ReplaceKeyWords(Match m) {

    return "<span class=highlight>" +m.Value+ "</span>";

}