问题描述
在文本框中输入值时,应突出显示与GridView内容匹配的值。我可以使用单个单词来实现此目的,但是当我在文本框中使用多个单词时,它将不起作用。任何帮助将非常感激。以下是我后面的C#代码:
foreach (GridViewRow gvr in GridView1.Rows) //loop each row
{
Label lblDescription = gvr.FindControl("lblDescription") as Label;
lblDescription.Text = Regex.Replace(lblDescription.Text,txtSearch.Text.Trim(),delegate (Match match)
{
return string.Format("<span style = 'background-color:#f30a31'>{0}</span>",match.Value);
},RegexOptions.IgnoreCase);
}
解决方法
foreach (GridViewRow gvr in GridView1.Rows) //loop each row
{
Label lblDescription = gvr.FindControl("lblDescription") as Label;
string searchtext = txtSearch.Text;
string[] searchtextList = searchtext.Split(' ');
foreach (string textlist in searchtextList)
{
lblDescription.Text = Regex.Replace(lblDescription.Text,textlist.Trim(),delegate (Match match)
{
return string.Format("<span style = 'background-color:#ee953f'>{0}</span>",match.Value);
},RegexOptions.IgnoreCase);
}
}