在做搜索时用repeater控件显示搜索到数据库的内容,想让搜索关键词高亮显示,如何在repeater中实现?
2个回答
展开全部
.aspx
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
.aspx.cs
//findtext是你打算高亮显示的关键字,这里没有做大小写处理
string findText = "Ch";
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label title = e.Item.FindControl("lblTitle") as Label;
if (title != null)
{
string text = title.Text;
text = text.Replace(findText, "<b>" + findText + "</b>");
title.Text = text;
}
}
}
<asp:Repeater ID="Repeater1" runat="server" onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
.aspx.cs
//findtext是你打算高亮显示的关键字,这里没有做大小写处理
string findText = "Ch";
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label title = e.Item.FindControl("lblTitle") as Label;
if (title != null)
{
string text = title.Text;
text = text.Replace(findText, "<b>" + findText + "</b>");
title.Text = text;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询