2008-6-4 08:06
handuner
如何加上搜索功能,并且和原来的排序结合
我现在只是写好了搜索功能,在代码的最下面我加了一个搜索btnserch_Click。但是不能很好的和排序结合在一起
下面是排序的程序:
C# codeprotected void Page_Load(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(Request.QueryString["sn"]) && string.IsNullOrEmpty(Request.QueryString["game"]))
{
BindGrid_null();
}
if (!string.IsNullOrEmpty(Request.QueryString["sn"]))
{
BindGrid_sn(Request.QueryString["sn"]);
}
if (!string.IsNullOrEmpty(Request.QueryString["game"]))
{
BindGrid_game(Request.QueryString["game"]);
}
if (tserch.Text.Trim() != "")
{
}
}
private void BindGrid_null()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
string cmd1 = "select game,sn from [order]";
SqlDataAdapter sda1 = new SqlDataAdapter(cmd1, con);
DataSet ds1 = new DataSet();
con.Open();
sda1.Fill(ds1, "order");
DataTable dt = ds1.Tables["order"];
con.Close();
foreach (DataRow row in dt.Rows)
{
Response.Write("<table width=250 height=20 border=0 cellspacing=0 cellpadding=0><tr><td width=100><a href=index.aspx?game=" + row["game"] + ">" + (row["game"]) + "</a></td><td width=150><a href=index.aspx?sn=" + row["sn"] + ">" + (row["sn"]) + "</a></td></tr></table>");
}
}
private void BindGrid_game(string game)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
string cmd = string.Empty;
if (string.IsNullOrEmpty(game))
cmd = "select game,sn from [order]";
else
cmd = "select game,sn from [order] where game= '" + game + "'union all select game,sn from [order] where game <> '" + game + "' ";
SqlDataAdapter sda = new SqlDataAdapter(cmd, con);
DataSet ds = new DataSet();
con.Open();
sda.Fill(ds, "order");
DataTable dt = ds.Tables["order"];
con.Close();
foreach (DataRow row in dt.Rows)
{
Response.Write("<table width=250 height=20 border=0 cellspacing=0 cellpadding=0><tr><td width=100><a href=index.aspx?game=" + row["game"] + ">" + (row["game"]) + "</a></td><td width=150><a href=index.aspx?sn=" + row["sn"] + ">" + (row["sn"]) + "</a></td></tr></table>");
}
}
private void BindGrid_sn(string sn)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
string cmd = string.Empty;
if (string.IsNullOrEmpty(sn))
cmd = "select game,sn from [order]";
else
cmd = "select game,sn from [order] where sn= '" + sn + "'union all select game,sn from [order] where sn <> '" + sn + "' ";
SqlDataAdapter sda = new SqlDataAdapter(cmd, con);
DataSet ds = new DataSet();
con.Open();
sda.Fill(ds, "order");
DataTable dt = ds.Tables["order"];
con.Close();
foreach (DataRow row in dt.Rows)
{
Response.Write("<table width=250 height=20 border=0 cellspacing=0 cellpadding=0><tr><td width=100><a href=index.aspx?game=" + row["game"] + ">" + (row["game"]) + "</a></td><td width=150><a href=index.aspx?sn=" + row["sn"] + ">" + (row["sn"]) + "</a></td></tr></table>");
}
}
protected void btnserch_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
string box = tserch.Text.ToString();
string cmd = "select game,sn from [order] where game='" + box + "' ";
SqlDataAdapter sda = new SqlDataAdapter(cmd, con);
DataSet ds = new DataSet();
con.Open();
sda.Fill(ds, "order");
DataTable dt = ds.Tables["order"];
con.Close();
foreach (DataRow row in dt.Rows)
{
Response.Write("<table width=250 height=20 border=0 cellspacing=0 cellpadding=0><tr><td width=100><a href=index.aspx?game=" + row["game"] + ">" + (row["game"]) + "</a></td><td width=150><a href=index.aspx?sn=" + row["sn"] + ">" + (row["sn"]) + "</a></td></tr></table>");
}
}