repeater绑定文字显示特定颜色

gaoweipeng7851 2009-08-09 11:01:28
我想做个检索的页面,检索的关键字显示为红色:比如说我检索“中国”
repeater控件:
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "QTitle").ToString()%>
</ItemTemplate>
</asp:Repeater>

QTitle有三条记录:中国很强大
我爱中国
中国人加油

我想实现显示数据时:只有中国现实特殊的颜色(比如红色)。其他还是黑色。

我在protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item)
{
for (int i = 0; i < Repeater1.Items.Count; i++)
{
Repeater1.Items[i].ToString().Replace("中国", "<font color='red'>'中国'</font>");
}
}
}
但是没有实现,想请教高手如何实现上面的效果。多谢!
...全文
236 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2009-08-09
  • 打赏
  • 举报
回复
<%# Eval("QTitle").ToString().Replace("中国","<font color='red'>中国</font>")%>

<%# Eval("QTitle").ToString().Contains("中国")>"<font color='red'>"+Eval("QTitle").ToString()+"</fomt>":Eval("QTitle").ToString())%>
逍遥游007 2009-08-09
  • 打赏
  • 举报
回复
Snowdust的方法应该可以,楼主试一下吧!
bobo_包子 2009-08-09
  • 打赏
  • 举报
回复
建议js写
Snowdust 2009-08-09
  • 打赏
  • 举报
回复
或者:
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound"> 
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "QTitle").ToString() %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lbl = (Label)e.Item.FindControl("Label1");
if (lbl != null)
{
lbl.Text = lbl.Text.Replace("中国", "<font color='red'>'中国'</font>");
}
}
}
IHandler 2009-08-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 snowdust 的回复:]
HTML code<asp:RepeaterID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound"><ItemTemplate><%# DataBinder.Eval(Container.DataItem,"QTitle").ToString().Replace("中国"," <font color='red'¡­
[/Quote]
这样,中国没必要加单引号
wulechang 2009-08-09
  • 打赏
  • 举报
回复
<%# DataBinder.Eval(Container.DataItem, "QTitle").ToString().Replace("中国", " <font color='red'>'中国' </font>") %>
就是这样
阿云ivan 2009-08-09
  • 打赏
  • 举报
回复
<%# DataBinder.Eval(Container.DataItem, "QTitle").ToString().Replace("中国", " <font color='red'>'中国' </font>") %>

这样就可以了,不需要处理Repeater1_ItemDataBound事件
Snowdust 2009-08-09
  • 打赏
  • 举报
回复
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound"> 
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "QTitle").ToString().Replace("中国", " <font color='red'>中国</font>") %>
</ItemTemplate>
</asp:Repeater>
wangan2008 2009-08-09
  • 打赏
  • 举报
回复
up~
flyerwing 2009-08-09
  • 打赏
  • 举报
回复
<%# DataBinder.Eval(Container.DataItem, "QTitle").ToString().Replace("中国", " <font color=\"red\">中国</font>") %>
hackervip1988 2009-08-09
  • 打赏
  • 举报
回复
顶顶
andrewsway 2009-08-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 gaoweipeng 的回复:]
引用 7 楼 bbs2241 的回复:
建议js写

请说的清楚些
[/Quote]

不建议写js,就用上面直接在绑定时判断的方法
gaoweipeng7851 2009-08-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 bbs2241 的回复:]
建议js写
[/Quote]
请说的清楚些
gaoweipeng7851 2009-08-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 snowdust 的回复:]
或者:
HTML code<asp:RepeaterID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound"><ItemTemplate><asp:LabelID="Label1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "QTitle").ToString() %>'></asp:Label></ItemTemplate></asp:Repeater>
C# codeprotectedvoid Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{if (e.Item.ItemType== ListItemType.Item|| e.Item.ItemType== ListItemType.AlternatingItem)
{
Label lbl= (Label)e.Item.FindControl("Label1");if (lbl!=null)
{
lbl.Text= lbl.Text.Replace("中国","<font color='red'>'中国'</font>");
}
}
}
[/Quote]

用label不行的。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧