asp.net把搜索结果的关键字以红色显示出来

    技术2022-05-11  16

    .aspx code

    Text to Search:

    The Data Being Search

    %# Highlight(searchword, DataBinder.Eval(Container.DataItem, "results"), "", "") %>

     

     

    .cs Code
    string searchword; DataTable pls = new DataTable(); public void Page_Load(object sender, EventArgs e) { BuildDataStore(); completeData.DataSource = pls; completeData.DataBind(); } public void BuildDataStore() { DataRow dr; pls.Columns.Add(new DataColumn("Results", typeof(string))); dr = pls.NewRow(); dr(0) = "If only God would give me some clear sign! Like making a large deposit in my name at a Swiss bank."; pls.Rows.Add(dr); dr = pls.NewRow(); dr(0) = "Is sex dirty? Only when it's being done right."; pls.Rows.Add(dr); dr = pls.NewRow(); dr(0) = "On the plus side, death is one of the few things that can be done just as easily as lying down."; pls.Rows.Add(dr); dr = pls.NewRow(); dr(0) = "I took a speed reading course and read 'War and Peace' in twenty minutes. It involves Russia."; pls.Rows.Add(dr); dr = pls.NewRow(); dr(0) = "To *you* I'm an atheist; to God, I'm the Loyal Opposition."; pls.Rows.Add(dr); dr = pls.NewRow(); dr(0) = "Sex without love is an empty experience, but as empty experiences go, it's a pretty good empty experience."; pls.Rows.Add(dr); dr = pls.NewRow(); dr(0) = "It's not that I'm afraid to die. I just don't want to be there when it happens."; pls.Rows.Add(dr); dr = pls.NewRow(); dr(0) = "What if everything is an illusion and nothing exists? In that case, I definitely overpaid for my carpet."; pls.Rows.Add(dr); dr = pls.NewRow(); dr(0) = "Interestingly, according to modern astronomers, space is finite. This is a very comforting thought-- particularly for people who can never remember where they have left things."; pls.Rows.Add(dr); dr = pls.NewRow(); dr(0) = "Eighty percent of success is showing up."; pls.Rows.Add(dr); } public void BindData(string strQuery) { searchword = strQuery; DataView dv; dv = new DataView(pls); dv.RowFilter = "Results LIKE '%" + Regex.Replace(searchword, "'", "''") + "%'"; SearchResults.DataSource = dv; SearchResults.DataBind(); } public string Highlight(string Search_Str, string InputTxt, string StartTag, string EndTag) { string ResultStr; return Regex.Replace(InputTxt, "//b(" + Regex.Escape(Search_Str) + ")//b", StartTag + "$1" + EndTag, RegExOptions.IgnoreCase); } public void searchQuotes(object sender, EventArgs e) { BindData(SearchTerm.Text); SearchResults.Visible = true; }

    最新回复(0)