下面的程式碼示範如何對一段文字進行多關鍵字查詢並高亮顯示,為自己做為一個小tip保留下
<%@ Page Language="C#" Debug="False" Strict="True" Explicit="True" Buffer="True"%>
<%@ Import Namespace="System" %>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.highlight {}{text-decoration:none; font-weight:bold; color:white; background:blue;}
</style>
<body bgcolor="#FFFFFF" topmargin="0" onLoad="document.forms[0].keywords.focus();">
<script language="C#" runat="server">
void Page_Load(Object Source, EventArgs E)
{
LabelTxt
。
written by hand, you need to make sure you have the special cases accounted for. You should loathe
hand-code because engineering time is extremely valuable, and to waste it on repetitive tasks is nearh tasks
. optimize the organization's most valuable assets.the creativity and enthusiasm of
the engineering team.";
}
public string Highlight(string Search_Str, string InputTxt)
{
Regex RegExp = new Regex(Search_Str.Replace(" ", "|").Trim(), RegexOptions.IgnoreCase);
return RegExp.Replace(InputTxt, new MatchEvaluator(ReplaceKeyWords));
RegExp = null;
}
public string ReplaceKeyWords(Match m)
{
return "<span class=highlight>" + m.Value + "</span>";
}
public void ButtonClick(Object sernder,System.EventArgs e )
{
LabelTxt.Text = Highlight(keywords.Text, LabelTxt.Text);
}
</script>
<H3></H3><BR>
<form runat="server" method="post">
<asp:TextBox id="keywords" runat="server"/>
<asp:Button id="button" Text="Submit" runat="server" OnClick="ButtonClick"/><br><br>
<asp:Label id="LabelTxt" runat="server"/>
</form>
</body>
</html>