[英]Highlight more than one word in my grid view
I need to highlight all word that user search for it inside my gridview 我需要突出显示用户在gridview中搜索的所有单词
I try this 我尝试这个
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;
}
and in my gridview Field I'm using it 在我的gridview字段中,我正在使用它
<asp:Label ID="Label6" runat="server"
Text='<%# Highlight(Eval("DocumentSummary").ToString()) %>'>
</asp:Label>
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>";
}
here is full code for your requirement. 这是您需要的完整代码。
The wrong was in ReplaceKeyWords dosen't need if...else 错误是在ReplaceKeyWords中不需要...否则
public string ReplaceKeyWords(Match m) {
return "<span class=highlight>" +m.Value+ "</span>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.