Friday, 27 March 2009

Cascading Filters – Dynamic Data Futures (Futures on Codeplex) (UPDATED)

This is the second article is the series following on from Cascading Filters – for Dynamic Data v1.0 all we are going to do is apply the same CascadingFilterTemplate to the DefaultFilter in the futures project.

  1. Dynamic Data (v1.0) .Net 3.5 SP1
  2. Dynamic Data Futures (Futures on Codeplex)
  3. Dynamic Data Preview 3 (Preview 3 on Codeplex)

This is the same class as in the previous article just added to a Dynamic Data Futures enabled website.

using System;
using System.Web.DynamicData;
using System.Web.UI;
using Microsoft.Web.DynamicData;


public partial class Default_Filter : CascadingFilterTemplate, ISelectionChangedAware
{
    public event EventHandler SelectionChanged
    {
        add
        {
            DropDownList1.SelectedIndexChanged += value;
        }
        remove
        {
            DropDownList1.SelectedIndexChanged -= value;
        }
    }

    public override string SelectedValue
    {
        get
        {
            return DropDownList1.SelectedValue;
        }
    }

    protected override void Page_Init(object sender, EventArgs e)
    {
        // remember to call the base class
        base.Page_Init(sender, e);

        // add event handler if parent exists
        if (ParentControl != null)
        {
            // subscribe to event
            ParentControl.SelectionChanged += ListControlSelectionChanged;
        }

        if (!Page.IsPostBack)
        {
            if (ParentControl != null)
                PopulateListControl(DropDownList1, ParentControl.SelectedValue);
            else
                PopulateListControl(DropDownList1);

            // Set the initial value if there is one
            if (DropDownList1.Items.Count > 1 && !String.IsNullOrEmpty(InitialValue))
            {
                DropDownList1.SelectedValue = InitialValue;
                RaiseSelectedIndexChanged(InitialValue);
            }
        }
        var c = Column;
    }

    // raise event
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        RaiseSelectedIndexChanged(DropDownList1.SelectedValue);
    }

    // consume event
    protected void ListControlSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        PopulateListControl(DropDownList1, e.Value);
        RaiseSelectedIndexChanged(DropDownList1.Items[0].Value);
    }
}

Listing 1 – Default.ascx.cs from Futures project filters

UPDATED: I’ve added this DropDownList1.Items.Count > 1 above to fix error mentioned

In Listing 1 I’ve marked up the changes in the Default.ascx.cs file.

<%@ Control 
    Language="C#" 
    CodeFile="Default.ascx.cs" 
    Inherits="Default_Filter" %>

<asp:DropDownList 
    ID="DropDownList1" 
    runat="server" 
    AutoPostBack="true" 
    EnableViewState="true" 
    CssClass="droplist" 
    onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    <asp:ListItem Text="All" Value="" />
</asp:DropDownList>

Listing 2 – only one change here the event handler for the dropdown list

Again in Listing 2 the only change is marked in BOLD ITELIC OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" this just allows us to fire the event when the selection changes as well as when notification of a change occurs.

I’ve used the same DB as in the previous article and the same metadata so you should get the same effect.

Next we will be jumping to the ASP.Net 4.0 Preview using DomainService and adding filtering to the new Filters.

Download (UPDATED)

1 comment:

Anonymous said...

Great job Steve, i am looking at all your work and its so helpful, i want to ask you if you have an estimated date for the next release, using ASP.Net 4.0 Preview with DomainService.
I am working with it, and it would be great to have your customizations for that.

Thanks!!