使用asp.net進行多關鍵字查詢的例子

來源:互聯網
上載者:User
下面的代碼示範了如何對一段文本進行多關鍵字查詢並高亮顯示,給自己做為一個小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.Text = "Give the proper respect to hand-coding.You should both respect and loathe handwritten code. You should 

respect it because there are often special cases integrated into code that are overlooked with a cursory inspection. When 

replacing code you’ve 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 nearly criminal. The goal 

of your generator should always be to 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>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.