ASP: How to create custom filters for your tabular data

Displaying data in a table is the most used and easiest way of publishing information for the user. It is quite common in web app and sites, that a query resulting recordset is displayed in a tabular form.
In this short article I will explain how to apply custom filters to the recordset, based on users requests. In order to show you how to do it, I need to explain a bit where to start. Our table could be something like:
IDPRODUCT FAMILYDESCRIPTIONCODECOLOURTYPE
1BottleGlass bottle1234GreenProduct
2BoxCarton box5678BrownPackage
The table could go on with all the products. We are going to create links inside the table to filter data.

The query
The above table is the result of a simple query. We need to write it down in order to fully understand the solution, so here it is our query:
"SELECT id, product_family, description, code, colour, type FROM products WHERE product_family like '" + product_var + "' AND colour like '" + colour_var + "' AND type like '" + type_var + "' ORDER BY id"
As you can see we are going to create 3 filters: for the product, the colour and the type.

Create the variables for the query
Just before running your query, place the necessary code to create the variables:
<%
Dim product_var
product_var = "%"
If (Request.QueryString("prod") <> "") then
    product_var = Request.QueryString("prod")
End If
%>
The code is just for the first variable. So let's do it for the other two variables:

<%
Dim colour_var
colour_var = "%"
If (Request.QueryString("colour") <> "") then
    colour_var = Request.QueryString("colour")
End If
Dim type_var
type_var = "%"
If (Request.QueryString("type") <> "") then
    type_var = Request.QueryString("type")
End If
%>
Ok. The point here is to set the where clause variables to "%" and, in case a QueryString is present, to the URL parameter passed.

The table
The table above will be something like:

 
  
  
  
  
  
  
 
      <%While (NOT rs.EOF)%>
 
  
  
  
  
  
  
 
      <%
       rs.MoveNext()
       Wend
      %>
IDPRODUCT FAMILYDESCRIPTIONCODECOLOURTYPE
<%=rs.fields.item("id").value%><%=rs.fields.item("product_family").value%><%=rs.fields.item("description").value%><%=rs.fields.item("code").value%><%=rs.fields.item("colour").value%><%=rs.fields.item("type").value%>
Assuming rs is your recordset.

The filter
In order to create the filter, we are going to add links to some elements of the table - specifically product_family, colour and type.
We are focusing on this line of the above snippet:
<%=rs.fields.item("product_family").value%>
And change it the following way.

     <%
       Dim pf
       pf = rs.fields.item("product_family").value
     %>
   <%=pf%>
We created a link to the same page (we assume the page we are working on is products.asp) adding an URL parameter called prod. When the link is clicked, the page will be reloaded passing the selected product family value as parameter for the query ("prod"). The result will be a page with filtered data.
You can do the same for the other two table columns.

Removing filter
In order to remove the filter, you can place in the column header - or wherever you like - another link (which might be an icon or a text like "Remove filter") with a code like:
Remove filter
Keeping URL parameters
Using the above code you will end up with a page where the 3 filters cannot be used together. In order to make that happen, we need to keep the URL intact for the other parameters when reloading the page. You can always recall an URL parameter by using Request.QueryString.
Change the links from:
<%=pf%>
to something like:
&type=<%= Request.QueryString("type")%>"><%=pf%>
The same you should do for the "Remove filter" links. For example, for removing the product family filter, the code should be:
&type=<%= Request.QueryString("type")%>">Remove filter

And that is all. I know it is just a start. Now use your imagination and improve the idea (like using a listbox to create the filter!).