I ran into this cool trick today while I was implementing filtering in a GridView.

The challenge was that I had to apply a composite FilterExpression to the GridView from the value of a DropDownList. I started out with the following (simplified) code:

<asp:DropDownList runat="server" ID="ddlFilter" AutoPostBack="true">
    <asp:ListItem Text="All images" />
    <asp:ListItem Text="Live images" />
    <asp:ListItem Text="Active images" />
    <asp:ListItem Selected="True" Text="Live and Active images" />
</asp:DropDownList>
<asp:GridView runat="server" DataSourceID="imagesDS">
    <Columns>
        <asp:BoundField HeaderText="Title" DataField="Title" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Image runat="server" ImageUrl='<%# Eval("Url") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:ObjectDataSource runat="server" ID="imagesDS" TypeName="ImageCollection" SelectMethod="GetImagesInfo"
</asp:ObjectDataSource>

And my Image object looks as follows:

public class Image
{
  public string Title { get; set; }
  public string Url { get; set; }
  public bool IsActive { get; set; }
  public bool IsLive { get; set; }
}

What had me stumped is that I needed to use different properties of the Image object in my FilterExpression. One thing I could do was override the Filtering event and perform my logic in there but as usual I had the impression that I could do better. And I could! Here’s how I modified my code:

I first added a FilterExpression property to my ObjectDataSource and linked that to the SelectedValue property of my DropDownList.

<asp:ObjectDataSource runat="server" ID="imagesDS" TypeName="ImageCollection" SelectMethod="GetImagesInfo"
    FilterExpression="{0}">
    <FilterParameters>
        <asp:ControlParameter PropertyName="SelectedValue" ControlID="ddlFilter" />
    </FilterParameters>
</asp:ObjectDataSource>

Not that the FilterExpression is set to {0} meaning that it will contain EXACTLY what will be in the SelectedValue property.

I then modified my DropDownList:

<asp:DropDownList runat="server" ID="ddlFilter" AutoPostBack="true">
    <asp:ListItem Value="" Text="All images" />
    <asp:ListItem Value="IsLive=True" Text="Live images" />
    <asp:ListItem Value="IsActive=True" Text="Active images" />
    <asp:ListItem Selected="True" Value="IsActive=True AND IsLive=True" Text="Live and Active images" />
</asp:DropDownList>

Notice how the Value property contains the FilterExpression we want to use for that selection. There’s really no limit to the expression you can put there. And of course you can use whichever control you want as the FilterParameter‘s source. You can also use an other Parameter type, such as SessionParameter, QueryStringParameter, etc.

I hope you will find this as useful as I have.

If you liked this post, please click on one of the advertisements below. Thanks!


0 Responses to “GridView Trickery: Using Complex FilterExpressions”

  1. No Comments

Leave a Reply


*