按名称搜索项目,并显示
问题描述:
我想通过使用它的名字以显示从数据库中的GridView的项目信息在另一页的内容,我想在GridView到另一个网页 我想这代码,,但它并没有在第一页按名称搜索项目,并显示
protected void Page_Load(object sender, EventArgs e)
{
}
public string txt
{
get
{
// Return the actual name if it is not null.
return TextBox1.Text ?? string.Empty;
}
}
}
在第二页
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=FATIMAH;Initial Catalog=makeup;Integrated Security=True");
string find = "select * from product where(name like '%' [email protected]+ '%')";
SqlCommand comm = new SqlCommand(find, con);
comm.Parameters.Add("@name", SqlDbType.NChar).Value = txt;
con.Open();
comm.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = comm;
DataSet ds = new DataSet();
da.Fill(ds, "name");
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
}
答
你可以传递参数使用查询字符串另一页。的onclick第一页上的按钮的情况下做到这一点: -
protected void Button1_Click(object sender, EventArgs e)
{
string search_word = TextBox1.Text.ToString();
Response.Redirect("~/secondpage.aspx?srch_word=" + search_word);
}
并在第二页的请求查询字符串: -
protected void Page_Load(object sender, EventArgs e)
{
string search = Request.QueryString["srch_word"];
//execute sql query to perform search operation
}
答
您的查询似乎离我上班
。尝试"select * from product where name like '%@name%'"
也为您的参数,你可以cmd.Parameters.AddWithValue("name", nameVariable);
不知道为什么你需要指定在这种情况下的类型。
答
你的问题不是SQL查询,你的问题是参数传递到另一个页面。 因为这个原因,你可以做到这一点在至少4种不同的方式。通过Cookie的
- 通过查询字符串发送文本
- 通过邮政传递数据是
- 它传递
- 通过会话
在这种情况下,你可以使用查询字符串发送,但你必须关心安全问题。 顺便说一句,这取决于你如何重定向到第2页。