0

I have a lookup screen with 25 fields. When the user clicks the search button, this information should be passed to the server, and the user should be redirected to a SearchResultList page. What is the best way to get the user selected values (from lookup screen) to the SearchResultList screen?

I can think of two ways:

  1. Add the fields to session
  2. Pass the selected values as query string

Is there a different better approach?

Note: In future, the look up screen may be converted into a popup div; still it will be redirecting to a different page for result.

Environment::

Visual Studio 2010

ASP.Net Web Forms

C#

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
LCJ
  • 22,196
  • 67
  • 260
  • 418

5 Answers5

3

could you post the form to the next page then then just get the values via Request.Form[""] on the search page?

Qpirate
  • 2,078
  • 1
  • 29
  • 41
1

Have you tried serializing the form using jQuery?

There are also a Bunch of other posts about this on SO.

Community
  • 1
  • 1
Dutchie432
  • 28,798
  • 20
  • 92
  • 109
1

I would recommend using a session and one session object at that. Something like this:

// When retrieving an object from session state, cast it to 
// the appropriate type.
ArrayList filterOptions = (ArrayList)Session["FilterOptions"];

// Write the modified list back to session state.
Session["FilterOptions"] = filterOptions ;
  • 1
    +1 That's how I'd do it. Quite often I also write a static class with static properties and the get/set methods read/write to the session. That way you don't need ugly casting and you have a central place for the session variable names. – Dave Becker Apr 02 '12 at 15:20
1

There is a 3rd, and better, option and that is to use POST rather than GET (which you would do when you put your parameters in the querystring).

ASPX uses POST automatically if you use a form like so:

<form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    ...
</form>

This is better because the data is passed to the page on postback using the HTTP POST method, which does not show the parameters in the querystring.

If, however, you want the user to be able to bookmark/favorite a search without programming, then using the GET (querystring) approach is best.

Using Session state to store search parameters is hard to test, hard to debug and generally a bad practice.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
1

For search, I typically prefer using GET with query strings. Very simple and transparent to the user. The only caveat is with your case of having 25 fields, you may run into the Url limit.

Look at the way Stackoverflow or Google does search. Only show parameters in the query string for non-default values. For example, if the user just fills in 1 of the 25 values, you should just pass that one parameter. For example: /search?q=term

Use POST for transaction based methods that update/save data. (Or if your Url will be too long with query strings.)

Avoid using session for it tends to have edge conditions that allow bugs to creep in. You have to worry about session timeouts, CDN caching, users switching between servers, etc. Why complicate your life when you don't need to.

Community
  • 1
  • 1
James Lawruk
  • 30,112
  • 19
  • 130
  • 137