I have the following scenario and am looking for the best way to structure things. I am finding lots and lots of conflicting advice online.

  • I have a user control on my home page for a simple search.
  • I have a user control on a search page for a detailed search.
  • I have a user control on a third page that displays search results in the same format, no matter which form was used (simple or detail) to begin the search.

I’ve tried several things that get me to the detail page, but am getting various errors getting the data from the calling form. Before I provide any of my broken code, I was hoping to start clean with thoughts and suggestions.

Thanks.

I know you have existing code so I do not know if this suggestion will provide a ton of help, but this is how I structure my search functions in a project. I will take a custom user control with a simple search box and button then add to it a simple Accordion control for displaying advanced features. This allows for both searches to be preformed from one user control and also limits the number of places you will have to update your code.
The display page could take parameters in many formats and it really depends the type of information to be displayed. For most search parameters you can get by with a Query String or an Application("") or ViewState("") variable(s). Be sure though that no matter what method you use to properly sanitize the query as to limit Injection or other forms of web based attacks.
One last thing that I would do is take the controls of these 3 and write a Search Class for them so again you can reduce the amount of places you have to modify your code and you have a nice reusable class for your next project that requires a search function.


Now that you have my take on the front end, lets take a peek at some code and see what we can come up with.

Using a panel would work well for a search page, by default show a simple search, or toggle to a detailed search on the same page. However, one of the places my simple search appears is on the site's home page so I don't see displaying the results on the home page an acceptable solution. This is one area, the user needs to be moved to another page to view the results.

In your search class/function you could check if your calling the search from default.aspx (or whatever your home is) and IF you are THEN httpcontext.current.responce.redirect("searchRes.aspx?query=blahblah&fields=field1.etc")

I sometimes use a Try Catch block when I know certian elements wont be on a page. So possibly do something similar to

TRY
SearchResultControl.text= SearchFn("field","query","database");
Catch ex As Exception
httpcontext.current.responce.redirect("searchRes.aspx?query=blahblah&fields=field1.etc")
end try


psudoCode /\

this if used in your panel code behind would display the results if SearchResultControl exists, if not it would throw an error and redirect to your search page. Maybe hackish, but thats how I would approach it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.