首先创建类库 Model (业务实体层),创建类: NewDina.CS 用于获取数据库相应字段名,
Demo如下:
[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
}
然后再创建一个类inaSet.CS该列用于将搜索的关键字放入数组,用于替换关键字用,令搜索的关键字显红色;
Demo如下:
[Copy to clipboard] [ - ]CODE:
public class DinaSet
{
private ArrayList dinarray = new ArrayList();
public ArrayList Dinarray
{
get
{
return dinarray;
}
}
}
类创建好之后就是界面设计了,
Demo如下:
[Copy to clipboard] [ - ]CODE:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txt" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="搜 索" onclick="btnSearch_Click" /><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>标题</td>
<td>内容</td>
<td>时间</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td colspan="3"></td>
</tr>
<tr>
<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>
</html>
后台代码如下:
[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('参数错误(不能为空)!')</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();
}
其中keyword_sousuo(string table_field, string keyword)方法的作用是返回条件查询语句的条件字符.
keyword_tag(string str,string keyword)方法的作用是将返回的结果数据中响应关键字进行替换,令查询的关键字显红色
本文作者: