asp.net(C#)+mssql2005站内搜索

ASP.NET(C#)+SQL2005实现站内搜索功能,请各位高手帮忙,我想在A页面上放一个textbox1用于输入搜索关键字和button1用于触发搜索事件,当点击button1时搜索结果显示在B页面上,result.aspx页面上用dalalist来显示数据。

请重点说明下select语句 和 result.aspx如何显示搜索结果.数据如何绑定

第一页的HTML代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
// <!CDATA[

function Submit1_onclick() {

}

// ]]>
</script>
</head>
<body>
<form id="form1" runat="server" action="result.aspx" method="post">
<div>
<asp:TextBox ID="TextBox1" runat="server" Width="172px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" Width="70px" />
</form>
</body>
</html>

第一页的C#代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string key = this.TextBox1.Text;

Response.Redirect("result.aspx?keyword="+key);
}
}

第二页的HTML代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="result.aspx.cs" Inherits="result" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Height="207px" Width="740px">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#FFFFC0" Font-Bold="True" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<FieldHeaderStyle BackColor="#FFFF99" Font-Bold="True" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:DetailsView>

</div>
</form>
</body>
</html>

第二页的C#代码:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class result : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string key = Request.Params["keyword"];

if (key != "")
{
string sql = "select * from titles where 1=1";
sql += " and Title like '%" + key + "%'";

SqlDataAdapter da = new SqlDataAdapter(sql, "Data Source=BRILLIANCE-LCG;Initial Catalog=pubs;Integrated Security=True");
DataSet ds = new DataSet();
da.Fill(ds);

DetailsView1.DataSource = ds.Tables[0];
DetailsView1.DataBind();
}
}
}
}

数据库用的是自带的pubs 查询的是titles表

第一页只是一个提供参数的.关键看第二页的代码.你问的几个问题是如何处理的
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

大家正在搜

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网