日期:2014-05-17  浏览次数:20464 次

Repeater绑定数据中关键字高亮问题!!!!!!!!!!
从网上搜了一段高亮代码 想绑定Repeater的时候让绑定中的内容变色 高手指点指点把。。
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Label lab = (Label)e.Item.FindControl("Label1");
        string a = lab.Text;
        Highlightkeywords(a);
    }
        /// 替换关键字为红色
        /// </summary>
        /// <param name="keycontent">原始内容</param>
        /// <param name="k">关键字,支持多关键字</param>
        /// <returns>String</returns>
        /// <author>haver Guo</author>
    public static string Highlightkeywords(string keycontent)
    {
        string resultstr = keycontent;
        string k = "公司";
        if (k.Trim().IndexOf(' ') > 0)
        {
            string[] myArray = k.Split(' ');
            for (int i = 0; i < myArray.Length; i++)
            {
                resultstr = resultstr.Replace(myArray[i].ToString(), "<font color=#FF0000>" + myArray[i].ToString() + "</font>");
            }
            return resultstr;
        }
        else
        {
            return resultstr.Replace(k, "<font color=#FF0000>" + k + "</font>");
        }
    }
------最佳解决方案--------------------
绑定的时候一个字段一个字加高亮:
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    ((Label)e.Item.FindControl("Label1")).Text=Highlightkeywords(((Label)e.Item.FindControl("Label1")).Text);//字段1
    ((Label)e.Item.FindControl("Label2")).Text=Highlightkeywords(((Label)e.Item.FindControl("Label2")).Text);//字段2
    .............
  }

public static string Highlightkeywords(string keycontent)
{
    string k = "公司";
    string resultstr = keycontent.Replace(k, "<font color=#FF0000>" + k + "</font>");
    return resultstr;
}
}
------其他解决方案--------------------
用js就可以实现的。
------其他解决方案-----