I have an ASP.NET page that contain a form that search the page.
Is there any solution so that I can have the search text in the URL?
I want to give the posi
There might be other better/cleaner/proper ways of doing it, like changing form's action, or changing button's PostBackUrl, but this is what I would do.
.aspx:
.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
return;
if (!string.IsNullOrEmpty(Request.QueryString["SearchTerm"]))
{
string searchTerm = Request.QueryString["SearchTerm"];
txtSearchTerm.Text = searchTerm;
DoSearch(searchTerm);
}
}
protected void btnSearch_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtSearchTerm.Text.Trim()))
{
Response.Redirect("~/Search.aspx?SearchTerm=" + txtSearchTerm.Text.Trim());
}
}
private void DoSearch(string searchTerm)
{
//search logic here
Response.Write("Search result: " + searchTerm);
}