![](/img/trans.png)
[英]Search redirect and display to current page and thereafter direct to another when item is chosen
[英]search an item by name and display the content in another page
我想通過使用它的名稱從數據庫中顯示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 '%' +@name+ '%')";
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();
}
}
您的查詢對我而言。 嘗試"select * from product where name like '%@name%'"
另外,對於您的參數,您只需cmd.Parameters.AddWithValue("name", nameVariable);
不確定為什么在這種情況下需要指定類型。
你的問題不是SQL查詢,你的問題是將參數傳遞給另一個頁面。 因此,您可以至少以4種不同的方式完成此操作。
在這種情況下,您可以使用查詢字符串,但您必須關心安全問題。 順便說一句,這取決於你如何重定向到第2頁。
您可以使用QueryString將參數傳遞給另一個頁面。 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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.