4
Vote

Default List foreign key filtering broken?

description

First: I really like this addition. It fills a need really well.
 
It may just be something I did wrong, but after I switched to the DynamicFilterRepeater I am no longer able to navigate from table to table with the default filtering methods.
 
I.e. it doesn't use the url-parameters to filter the dataset.
 
Example:
I have two tables Products and Orders that are linked on a foreign key to product_id from products.
 
The default List.aspx page for Products shows me a link to "View Orders" which navigates to the Orders list with ?product_id=xyz.
 
Using the default filtering mechanism, the Order list would now be limited to showing only orders for product "xyz". However with the DynamicFilterRepeater it shows all orders, even if I specify a [Filter(FilterMode=FilterControlMode.Equals)] on the Order tables product_id column.
 
I am guessing it has more to do with the DynamicLinqDatasource than the DynamicFilterRepeater. Does this make sense?

comments

jheyse wrote Oct 18, 2008 at 3:44 PM

Ah... Nice catch. Yes the DynamicFilterRepeater will prevent foreign key navigation from working. The issue is that the ForeignKey FilterTemplateUserControl doesn't know to look at the query string for a value.. This would be solvable by adding some code to the ForeignKey's Page_Load method to check the query string for the correct value and apply that.

So it would look something like

void Page_Load()
{
if(!Page.IsPostBack)
{
   //do some validation 
   this.Value = Page.QueryString[this.Column.Name"].Value;
}
}

I think the other issue will be notifying the DynamicFilterRepeater/DynamicFilterForm that the Filter Value has changed and it should perform the search again. This could be done by implementing an event or climbing the control hierarchy until you find a DynamicFilterContainerBase and calling a Search method. Unfortunatley it doen't have a public Search method right now. That would have to be added.

Stevek wrote Nov 7, 2008 at 1:36 PM

Josh, are you able to provide a working sample that resolves this problem? The ability to browse from a primary table to a foreign table was a great piece of functionality in the original DynamicData implementation. It would be a shame if we lose it with your otherwise excellent DDF contribution.

Stevek wrote Nov 7, 2008 at 10:22 PM

The following is not the most elegant solution, but it works on my /list.aspx pages:
using System;
using System.Collections.Specialized;
using System.Linq;
using System.Web.DynamicData;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
protected void Page_Load(object sender, EventArgs e)
{
    table = GridDataSource.GetTable();
    //Actual code shortened for brevity

    if(!Page.IsPostBack)
    {
        NameValueCollection parameters = new NameValueCollection();
        NameValueCollection queryString = Page.Request.QueryString;
        foreach (var key in queryString.AllKeys)
        {
            GridDataSource.WhereParameters.Add(key, table.GetColumn(key).TypeCode, queryString[key]);
        }
    }
}

The code parses the client side query string and adds parameters to the page to filter the records accordingly.

The only thing missing now is to update the DynamicFilterForm's Clear function such that it removes the ClientQueryString.

wrote May 7, 2009 at 10:37 PM

wrote Nov 6, 2009 at 12:44 PM

justaboy84 wrote Oct 19, 2010 at 9:30 AM

In additon to adding

if (!Page.IsPostBack)
        {
            NameValueCollection parameters = new NameValueCollection();
            NameValueCollection queryString = Page.Request.QueryString;
            foreach (var key in queryString.AllKeys)
            {
                GridDataSource.WhereParameters.Add(new Parameter(key, table.GetColumn(key).TypeCode, queryString[key]));
            }
        }
to the Page__Load method of the List.aspx.cs and adding

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //do some validation
            this.Value = Page.Request.QueryString[this.Column.Name];
        }
    }
to the ForeignKey.ascx.cs I have a workarround to let the Search and Clear buttons work correctly:

Add the following eventhandles to the List.aspx.cs:

// to clear the parameter value that was added from the querystring that is still part of the whereParameters collection
protected void SearchButton_Command(object sender, CommandEventArgs e)
    {
        NameValueCollection parameters = new NameValueCollection();
        NameValueCollection queryString = Page.Request.QueryString;
        foreach (var key in queryString.AllKeys)
        {
            foreach (Parameter item in GridDataSource.WhereParameters)
            {
                if (item.Name == key)
                {
                    GridDataSource.WhereParameters.Remove(item);
                    break;
                }
            }
        }
    }

    // to reload the page so all filters are reset
    protected void ClearButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("List.aspx", true);
    }
On the List.aspx locate the SearchButton and add OnCommand="SearchButton_Command".

On the List.aspx locate the ClearButton and add OnClick="ClearButton_Click" and remove CommandName="Clear".

I hope this is helpfull.

wrote Oct 19, 2010 at 9:31 AM

wrote Feb 14, 2013 at 3:41 AM