Question

On my search.aspx I'm using a ListView to display search results:

Markup:

<asp:TextBox runat="server" ID="txtSearch" placeholder="search" />

<asp:Button runat="server" ID="btnSubmit" Text="Search" OnClick="btnSubmit_Click" />

<asp:ListView ID="ListView1" runat="server">

    <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" runat="server" ImageUrl='<%# String.Format("../Images/books/{0}.jpg", Eval("Id").ToString()) %>' NavigateUrl='<%#  "../books/bookdetails.aspx?BookId=" + Eval("Id").ToString() %>'></asp:HyperLink>

        <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Publisher") %>' />
        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Author") %>' /></p>
    </ItemTemplate>

</asp:ListView>

Code Behind:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    using (ELibraryEntities entities = new ELibraryEntities())
    {
        var search = from books in entities.Reviews
                    where books.Title.Contains(txtSearch.Text.Trim())
                    select books;
        ListView1.DataSource = search;
        ListView1.DataBind();
    }
}

and it works fine and the search results are displayed in the search.aspx page.

Now, I want to move the search textbox <asp:TextBox runat="server" ID="txtSearch" placeholder="search" /> to the MasterPage, so when a user search, the search results are opened on the new search.aspx page.

Was it helpful?

Solution

Microsoft has a good article on passing values between ASP .NET on MSDN which I would recommend reading. In your current setup, you are taking an action on a page, then based on that action you are running code which retrieves data from an element on the page and performing another action. In order to split the search box and search results, you will need to ensure the action gets communicated across both pages and the content (search query) gets communicated across both pages.

As a summary, there are a few methods you can take:

  • Use URL query string parameters *This is the most used for search!
  • HTTP POST the data to the new page
  • Pass the parameters server side (eg. session state)

I would recommend reading into the query string method. As a summary:

//1) On the source page with the search box, you would redirect the user 
//     to the search results page and append the search query to the URL: 
Response.Redirect("search.aspx?search=" + HttpUtility.UrlEncode(query));

//2) On the search results page, parse the query string values
String searchQuery = Request.QueryString["search"];

//3) Perform your search action in code and display the results.
protected void btnSubmit_Click(object sender, EventArgs e){
    //your search code
    using (ELibraryEntities entities = new ELibraryEntities())
    {
        var search = from books in entities.Reviews
                where books.Title.Contains(searchQuery)
                select books;
        ListView1.DataSource = search;
        ListView1.DataBind();
    }
}

Note the use of Response.Redirect, HttpUtility.UrlEncode, and Request.QueryString.

OTHER TIPS

Use post-redirect-get:

  1. POST: Have your master page handle the click event
  2. REDIRECT: Then redirect to the search page, including the search parameters in the search URL
  3. GET: Move the logic in the search page's btnSubmit_Click to its page load event, taking into account the search terms passed via the URL instead of posted directly from a text box.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top