best way to display search results on the same page as the search control in ASP.Net

元气小坏坏 提交于 2019-12-04 19:03:32

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 :)

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);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!