求解释下搜索功能代码?
搜索是通过一个自定义控件来实现的src.ascx.cs
protected void Button1_Click(object sender, EventArgs e)
{
string whe = Server.HtmlEncode(scrtb.Text.ToString());
switch (scrddl.SelectedValue)
{
case "name":
Response.Redirect("screch.aspx?sql=name&key="+whe);
break;
case "title":
Response.Redirect("screch.aspx?sql=title&key=" + whe);
break;
case "concent":
Response.Redirect("screch.aspx?sql=concent&key=" + whe);
break;
}
}
搜索结果页放了个gridview
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fill();
}
}
public void fill()
{
string sql = Request.QueryString["sql"];
string whe = Request.QueryString["key"];
string que = "";
switch (sql)
{
case "name":
que += ("select * from guest where name like '%" + whe + "%' order by id Desc");
break;
case "title":
que += ("select * from guest where title like '%" + whe + "%' order by id Desc");
break;
case "concent":
que += ("select * from guest where concent like '%" + whe + "%' order by id Desc");
break;
}
if ((odb.ds(que)).Rows.Count <= 0)
{
Response.Write("未找到你想要的,<a href=javascript:history.back()>返回</a>重新来过吧!");
}
else
{
GridView1.DataSource = odb.ds(que);//odb是数据库链接类
GridView1.DataBind();
}
}
------解决方案--------------------protected void Button1_Click(object sender, EventArgs e)
{
string whe = Server.HtmlEncode(scrtb.Text.ToString());//获取搜索的关键字
switch (scrddl.SelectedValue) //选择搜索的选项:名称(name),标题(title),内容(content)
{
case "name":
Response.Redirect("screch.aspx?sql=name&key="+whe);
break;
case "title":
Response.Redirect("screch.aspx?sql=title&key=" + whe);
break;
case "concent":
Response.Redirect("screch.aspx?sql=concent&key=" + whe);
break;
} //选择好后,跳转到screch.aspx页面,传递sql,key参数
}
--------------------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fill(); //显示查找的数据
}
}
public void fill()
{
string sql = Request.QueryString["sql"];
string whe = Request.QueryString["key"]; //获取传递的两个参数,sql和key
string que = "";
switch (sql)//跟据查找的类型写出查询语句,这里的查询是模糊查询并且以id字段进行降序排列