public class dinaset
{
private arraylist dinarray = new arraylist();
public arraylist dinarray
{
get
{
return dinarray;
}
}
}
<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="搜 索" /><br />
<asp:datalist id="dldata" runat="server" height="158px" width="100%">
<headertemplate>
<table border="0" cellpadding="0" cellspacing="0" width="100%" >
<tr>
<td>标题</td>
<td>内容</td>
<td>时间</td>
</tr>
</headertemplate>
<itemtemplate>
<tr>
<td colspan="3"></td>
</tr>
<tr>
<td ><%...# 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>
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();
}