我想在查询结果中把我查询的模糊关键字变色显示,比方说 在前台页面输入fd,在前一个页面把包含fd的所有数据字段(标题 ,内容)变色显示,但是现在有一个问题,如果是 fd连在一起的话,就能变色显示。但 f d中间有一个空格的话,就不能变色显示。 代码如下:
public void fillDataList()
{
OleDbConnection cn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source="+Server.MapPath("../data/Wenda.mdb")); cn.Open();

TextBox2.Text=Request.QueryString["suo"];
string   str=TextBox2.Text.Replace(" ","|"); string[] arrStr=str.Split('|');
string   mysql="select Wid,problem, content  from   problems   where   1=1";   
for(int   i=0;i<arrStr.Length;i++)   
{   
mysql   +=   "   And   problem   like   '%"   +  arrStr[i]   +   "%'";   
}   
     OleDbDataAdapter da=new OleDbDataAdapter(mysql,cn);
DataSet ds=new DataSet(); da.Fill(ds,"problems");   

            
for(int i=0;i<ds.Tables["problems"].Rows.Count;i++)   
{

if(SerchKey !=null)
{
ds.Tables["problems"].Rows[i]["problem"]=(string)ds.Tables["problems"].Rows[i]["problem"].ToString().Replace(SerchKey,"<font color='#FF0000'>"+SerchKey+"</font>"); 
ds.Tables["problems"].Rows[i]["content"]=(string)ds.Tables["problems"].Rows[i]["content"].ToString().Replace(SerchKey,"<font color='#FF0000'>"+SerchKey+"</font>");  
}
} this.DataList1.DataSource =ds;
this.DataList1.DataBind ();
cn.Close (); }
Request.QueryString["suo"] 从上一个页面传来的值  表名 problems  数据字段 Wid,problem,content  
希望大家能帮我解决这个难题!谢谢大家了!   

解决方案 »

  1.   

    DataList1绑定数据时使用Replace("","<font color='red'</font>");替换数据
    e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace(“”, " <span style='color:red'>" + keyWord + " </span>"); 或 <ItemTemplate> 
            <%# Eval("Title").ToString().Replace("", " <font color='red'></font>") %> 
        </ItemTemplate> 
      

  2.   

    DataList1绑定数据时使用Replace(""," <font color='red' </font>");替换数据 
    e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace(“”, " <span style='color:red'>" + keyWord + " </span>"); 或 <ItemTemplate> 
            <%# Eval("Title").ToString().Replace("", " <font color='red'> </font>") %> 
        </ItemTemplate> 说的这个我就不明白啦 
    DataList1绑定数据时使用Replace(""," <font color='red' </font>");替换数据 
    e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace(“”, " <span style='color:red'>" + keyWord + " </span>"); 
    要写那呀 是写在那的类呀还有就是 
    这个<ItemTemplate> 
            <%# Eval("Title").ToString().Replace("", " <font color='red'> </font>") %> 
        </ItemTemplate>
    报错 字符串的长度不能为零。
    参数名: oldValue