0

我需要突出显示用户在我的 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>
4

2 回答 2

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>";
    }

这是满足您要求的完整代码。

于 2013-06-29T05:58:12.180 回答
0

错误在于 ReplaceKeyWords dosent need if...else

public string ReplaceKeyWords(Match m) {

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

}
于 2013-06-29T06:13:14.230 回答