asp.net 如何改变GridView搜索出来关键字的颜色?就像百度似的,我获得的是一个集合,改变集合中关键字的集合

 输入“产量”查询,将“产量”拆分,产、量

string strColNameCnDt = string.Empty;
            for (int j = 0; j < dtTemp.Rows.Count; j++)
            {
                strColNameCnDt = dtTemp.Rows[j]["colnamecn"].ToString();
                strKeyWordCopy = strKeyWord;
                while (strKeyWordCopy.Length > 1)
                {
                    str = strKeyWordCopy.Substring(0, 1);拆分
                    strKeyWordCopy = strKeyWordCopy.Substring(1, strKeyWordCopy.Length - 1);
                    if (strColNameCnDt.Contains(str))
                    {
                        string newStr = "<font color='red'>" + str + "</font>"; 增加颜色
                        strColNameCnDt = strColNameCnDt.Replace(str, newStr);
                    }
                }
                str = strKeyWordCopy;strKeyWordCopy为一个长度
                if (strColNameCnDt.Contains(str))
                {
                    string newStr = "<font color='red'>" + str + "</font>";
                    strColNameCnDt = strColNameCnDt.Replace(str, newStr);
                }
                dtTemp.Rows[j]["colnamecn"] = strColNameCnDt;
            }

GridView中ID:gvResults

 gvResults.DataSource = dtTemp; 绑定数据
 gvResults.DataBind();

以下转载自:百度回答

 <script language="JavaScript">
//key
:百度|新浪|搜狐 格式
function highlight(key) {
var key = key.split('|');
for (var i=0; i<key.length; i++) {
var rng = document.body.createTextRange();
while (rng.findText(key[i]))
//替换
rng.pasteHTML("<font style ='background-color :Yellow;' color='red'>"+rng.text+"</font>");
//rng.pasteHTML(rng.text.fontcolor('red'));
}
}
</script>
原文地址:https://www.cnblogs.com/xuxin-1989/p/3925022.html