从网上搜了一段高亮代码 想绑定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>");
        }
    }

解决方案 »

  1.   

    恩,不需要后台弄 用JS就实现了function HighlightKey(key)
    {

    $(".searchlist").each(function(){
    $(this).html($(this).html().replace(key,"<font color='#FF0000'>"+key+"</font>"));

    })
    }
      

  2.   

    这个KEY 就是关键字?
    我想把Repeater绑定的数据中的符合关键字的 加高亮 怎么实现
      

  3.   

    绑定的时候一个字段一个字加高亮:
    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;
    }
    }