views:

902

answers:

3

I have two controls on a page, one is a search entry and submission control the other is the search results control, they have to be separated due to the layout of the site. Im looking at different ways of having the search control submit displaying the search results in the results control, initially im not bother about postbacks, im more bothered about two things

1) How do i pass the selected search terms back to the same page (or indeed to the control that displays the results) QUerystring, hiddenfield, session, viewstate (effectively hidden field)?

2) how do i actually make the page submit to itself.

apologies if this is a bit basic but im not an ASP.Net expert

A: 

Think this is pretty much what your looking to do:

http://www.java2s.com/Code/ASP/Development/SubmittinganddisplayingformvaluesinASPNET.htm

James.

James
its vaguely similar but i am using ascx files and asp.net controls
Matt
+2  A: 

There are many, many ways for doing what you are asking and to be honest I'm not sure that there is a right answer. It all depends on your development style and the problem that you are trying to solve.

I have a fair amount of experience with regards to writing search interfaces and in my opinion using the query string is a good way of passing search criteria. However this has its advatages and its disadvantages.

Pros:

  • You can get to a search results page from anywhere in your site or an external site simply by passing a query string.

  • Your search results can be crawled easily as they are just pages on your site.

  • Query strings are fairly simple to use.

  • The user can see what it is they are searching for in the query string. (However this might be a bad thing depending its use.)

  • Users can book mark thier serach results (Thanks Fredrik Mörk)

Cons:

  • You might have to take the time to make your search criteria look pretty. I.E. Do you want your users just to see an id that means nothing to them?

  • You may have problems html encoding your search criteria as they will need to be passed as a query string argument. Encoding can be done easily but it can cause you a headache if you are using reserved characters for other things.

  • Your users can see what they are searching for. (Like I said this might be a good or a bad thing.)

  • Your query string might become far too long.

There are probably some other things that I have not thought of but that gives you an idea.

As for posting a page back to itself again there are a few ways of doing this but try a submit button or anchor tag with a # sign as the href attribute.

Another thing to think about might be using a url rewriter to make your ugly query string a nice looking URL.

For example:

used_car_search.aspx?make=ford&model=focus

Could become:

used_ford_focus.html

Happy coding :)

lexx
I would add one more thing on the Pros side; in my opinion the most important: the search result becomes bookmarkable. That is (in my eyes) a very important feature of a good search interface.
Fredrik Mörk
@Fredrik Mork - Good point.
lexx
thanks, i had averted myself from querystrings but the pros outweight the cons from my standpoint
Matt
+1  A: 

These are UserControls, correct? I think the best way to communicate among UserControls is to use events. Create your own EventArgs class to encapsulate the search criteria. The submission control raises an event when a search is submitted, and the containing page handles the event and calls a method on the search results control to perform the search and display results.

Alternately, the results control could just be responsible for displaying a collection of objects and the search control would actually execute the search and return the collection in the EventArgs.

Here's an example from a master-detail set of UserControls. The ProjectList UserControl raises an event when a project is selected:

public event EventHandler<ProjectSelectedEventArgs> ProjectSelected;

    protected void uxProjectList_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        if (ProjectSelected != null)
        {
            var keys = uxProjectList.DataKeys[uxProjectList.SelectedIndex].Values;
            var projectId = (Guid)keys[0];

            var args = new ProjectSelectedEventArgs(projectId);
            ProjectSelected(this, args);
        }
    }

The container page handles the event and calls a method on the ProjectDetail UserControl to display details for the project.

    protected void uxHeroProjectList_ProjectSelected(object sender, ProjectSelectedEventArgs e)
    {

        uxProjectDetails.Visible = true;
        uxProjectDetails.DisplayDetails(e.ProjectId);
    }
Jamie Ide