asp.net search matching keyword for red display code

Source: Internet
Author: User
Tags eval split tostring
Asp.net| Search | show | keywords | match first create class library Model (business entity layer), create class: NewDina.CS to get the corresponding field name of the database.

Demo as follows:

[Copy to Clipboard] [ - ] Code:public class Newdina
{
#region Fields
private string _news_title = String.Empty;
private string _news_content = String.Empty;
private string _news_time;
#endregion
Public Properties#region Public Properties
public string News_title
{
get {return _news_title;}
set {_news_title = value;}
}
public string News_content
{
get {return _news_content;}
set {_news_content = value;}
}
public string News_time
{
get {return _news_time;}
set {_news_time = value;}
}

#endregion
}
And then create a class InaSet.CS the column is used to put the search keyword in an array to replace the keyword, making the search keyword red;

Demo as follows:

[Copy to Clipboard] [ - ] Code:public class Dinaset
{
Private ArrayList Dinarray = new ArrayList ();
Public ArrayList Dinarray
{
Get
{
return dinarray;
}
}
}
After the class is created, the interface is designed,

Demo as follows:

[Copy to Clipboard] [ - ] CODE: <title> Untitled Page </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:textbox id= "TXT" runat= "server" ></asp:TextBox>
<asp:button id= "btnsearch" runat= "server" text= "search"/><br/>
<asp:datalist id= "Dldata" runat= "Server" height= "158px" width= "100%" >
<HeaderTemplate>
<table border= "0" cellpadding= "0" cellspacing= "0" width= "100%" style= "height:100%" >
<tr>
<td> title </td>
<td> content </td>
<td> Time </td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
&LT;TD colspan= "3" ></td>
</tr>
<tr>
&LT;TD style= "width:200px" ><%...# Eval ("News_title")%></td>
<td><%...# Eval ("News_content")%></td>
<td><%...# Eval ("News_time")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
</div>
</form>
</body>
The background code is as follows:

[Copy to Clipboard] [ - ] Code:public string Keyword_sousuo (string table_field, string keyword)
{
String Str01= "", str02= "", keyword_words;
string[] keywords, table_fields;
Table_fields = Table_field. Split (', ');
Keywords = keyword. Split (', ');
if (Table_field!= "")
{
Str01 = "(" + table_fields[0]. ToString () + "like '%" + keyword + "% '" + ")";
for (int i = 0; i < Table_fields. Length; i++)
{
Str01 = Str01 + "or" + table_fields[i]. ToString () + "like '%" + keyword + "%";
}
Str01 = Str01 + ")";
}
Else
{
Response.Write (' <script>alert (' parameter error (cannot be empty)! ') </script> ");
}
Keyword = keyword. Replace ("", "");
Keywords = keyword. Split (")";
if (keywords. Length > 0)
{
for (int i = 0; i < keywords. Length; i++)
{
Str02 = Str02 + "or" + table_fields[0]. ToString () + "like '%" + keywords[i]. ToString () + "% '";
for (int j = 1; j < Table_fields. Length; J + +)
{
Str02 = Str02 + "or" + table_fields[j] + "like '%" + keywords[i]. ToString () + "% '";
}
Str02 = Str02 + ")";
}
Str02 = "(" + Str02. Replace (") (", ") and (") + ")";
Keyword_words = "(" + Str01 + "" + Str02 + ")";
}
Else
{
Keyword_words = Str01;
}
return keyword_words;
}
public string Keyword_tag (string str,string keyword)
{
String Str01, Str02;
string[] keywords;
String keyword_tag = "";
Keyword = keyword. Replace ("", "");
Str01 = str. Replace (keyword, "<font color=" #ff0000 ">" +keyword+ "</font>");
Keywords=keyword. Split (")";
if (keywords. Length > 0)
{
Str02 = str;
for (int i = 0; i < keywords. Length; i++)
{
Str02 = Str02. Replace (Keywords[i], "<font color=" #ff0000 ">" + keywords[i] + "</font>");
}
Keyword_tag = Str02;
}
Else
{
Keyword_tag = Str01;
}
return keyword_tag;
}
protected void btnSearch_Click (object sender, EventArgs e)
{
string keyword = txt. Text;
String sql_where = Keyword_sousuo ("news_title,news_content,news_time", keyword);
String strconn = "server=;" User Id=sa; Password=123456;database=r; ";
SqlConnection conn = new SqlConnection (strconn);
Conn. Open ();
String strSQL = "Select News_title,news_content,news_time from Cmgr_newsdiary where" + Sql_where + "ORDER by news_id";
SqlDataAdapter SDA = new SqlDataAdapter (Strsql,conn);
DataSet ds = new DataSet ();
Sda. Fill (DS);
DataTable dt = ds. Tables[0];
Modeldata.dinaset NDS = new Modeldata.dinaset ();-_!!
foreach (DataRow dr in Dt. Rows)
{
Modeldata.newdina NDA = new Modeldata.newdina ();
Nda.news_title =keyword_tag (dr["News_title"). ToString (), txt. Text);
Nda.news_content =keyword_tag (dr["News_content"). ToString (), txt. Text);
Nda.news_time =keyword_tag (dr["News_time"). ToString (), txt. Text);
Nds. Dinarray.add (NDA);
}
Dldata.datasource = NDS. Dinarray;
Dldata.databind ();
Conn. Close ();
}
Where the Keyword_sousuo (string table_field, string keyword) method acts as a conditional character that returns a conditional query statement.

The function of the Keyword_tag (string str,string keyword) method is to replace the response keyword in the returned result data so that the query's keyword is red

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.