Search code examples
asp.netdynamic-dataasp.net-dynamic-data

ASP.NET Dynamic Data: Display a dynamic list of filter criteria in a side bar


I have a set of categories that a user can choose from. Each category has a different set of properties that a user may want to filter on.

The items in each category are displayed in a grid view. Each category has its own web page for the grid view.

When the grid view is displayed I would like a side bar to display properties that pertain to the category. The user should be able to select a property to filter. And filter by min/max values on the property.

I'm trying to determine what controls should go in the sidebar and also how to dynamically populate the set of controls (assuming each one is a distinct property filter).

For example looking at Amazon books the sidebar has a dynamically generated list of filters that pertains to the category of books.

Other nice features would be:

  • Change the list of properties so that only properties/filters that will produce a result will be displayed.

  • Have each property/filter show the number of results that will be displayed if selected.


Solution

  • I don't really know how you're going to load the gridview, but this is how I was able to do something similar.

    Let's say you're getting your data to the GridView via SQL query:

    select property1, property2, property3, ...., from categoryA
    

    Whatever is on the side view should factor into your SQL query somehow, each of them with auto post back.

    <asp:TextBox runat="server" AutoPostBack="true" ID="Property1" /> 
    

    So when it posts back to the server, in your page load method:

    protected void Page_Load(object sender, EventArgs e) 
    {
        if(IsPostBack)
        { 
            UpdateCategoryQuery();
        }
    }
    

    And on your UpdateCategoryQuery() method:

    private void UpdateCategoryQuery()
    {
        if(Property1.Text != "") 
        {
            string sql = "where property1 = '" + Property1.Text + "'";
        }
        //... and go on down the list.   
    }  
    

    Finally, you will want to read this query and bind the data to the GridView using .DataSource and .DataBind();

    It's an extremely simple example, but I didn't really know exactly what you were looking for, so I hope this helps you along.

    Edit: The query here can get fairly complicated depending on how many properties you have to filter the data, so you may have to spend some time building it out to make sure it runs correctly.