2013-06-29 25 views
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> 
+0

它是在TextBox1中一个字工作... – AAHN

回答

0

的错误是ReplaceKeyWords 这么想的需要IF ... ELSE

public string ReplaceKeyWords(Match m) {

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

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

这里是您的要求全部代码。

+0

我删除,但同样的...代码对于一个单词正确工作 – AAHN

相关问题