您的位置:首页 > 编程语言 > ASP

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

2014-08-20 16:56 549 查看

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

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: