Set the Default Value of a FieldFilter on a Show Table Page

"Normally, a Show Table page displays all records in a database table or view when the default value in the FieldFilter is set to "All". Discover how to display records specific to some value in a FieldFilter when the page is initially displayed."
- Anh Trinh, Software Engineer, Iron Speed, Inc.

January 17, 2007
Iron Speed Designer V4.1

Introduction
Iron Speed Designer automatically creates dropdown lists on Show Table pages known as "FieldFilters". A FieldFilter control filters a table of records based on the value selected in the FieldFilter control and the values contained in the associated database field.

Normally, a Show Table page displays all records in a database table or view when the default value in the FieldFilter is set to "All". Sometimes you might want to display records specific to some value in a FieldFilter when the page is initially displayed. The sample code shown below sets the default value of a FieldFilter on a Show Table page and filters the data based on the default value set for the FieldFilter.

You can set the default value of a filter when a page loads the first time and filter the data based on the default value. Override the CreateWhereClause() method in the TableControl class and set the item selected in the FieldFilter. Then, override the PopulateTableFilter() method in the TableControl class to filter the page.

In the following example, the TableControl filters only rows whose CustomerID is "abc".

Procedure
Step 1: Create an application based on the Orders table in the Northwind database.

Step 2: Override the CreateWhereClause() and PopulateCustomerIDFilter() methods in the TableControl class.

For .NET Framework 1.1, add your code customization in the OrdersTableControl class of ShowOrdersTablePage.aspx.cs, located in

...\<Application Folder>\Orders\ShowOrdersTablePage.aspx.cs or .vb

For .NET Framework 2.0 and 3.0, add your code customization in the OrdersTableControl class of ShowOrdersTablePage.Controls.cs, located in

...\<Application Folder>\App_Code\Orders\ShowOrdersTablePage.Controls.cs or .vb

C#:

public override WhereClause CreateWhereClause()
{
    WhereClause wc = base.CreateWhereClause();
    if ((wc == null))
    {
        wc = new WhereClause();
    }
    if (!this.Page.IsPostBack)
    {
        wc.iAND(OrdersTable.CustomerID, BaseFilter.ComparisonOperator.EqualsTo, "abc");
    }
    return wc;
}
protected override void PopulateCustomerIDFilter(string selectedValue, int maxItems)
{
    if (!this.Page.IsPostBack)
        base.PopulateCustomerIDFilter("abc", maxItems);
    else
        base.PopulateCustomerIDFilter(selectedValue, maxItems);
}

Visual Basic .NET:

Public Overrides Function CreateWhereClause() As WhereClause
    Dim wc As WhereClause = MyBase.CreateWhereClause
    if (wc Is Nothing) Then
        wc = New WhereClause
    End If
    If Not Me.Page.IsPostBack Then
        wc.iAND(OrdersTable.CustomerID, BaseFilter.ComparisonOperator.EqualsTo, “abc")
    End If
        Return wc
End Function
 
Protected Overrides Sub PopulateCustomerIDFilter(ByVal selectedValue As String, ByVal maxItems As Integer)
    If Not Me.Page.IsPostBack Then
        MyBase.PopulateCustomerIDFilter("abc", maxItems)
    Else
        MyBase.PopulateCustomerIDFilter(selectedValue, maxItems)
    End If
End Sub

Step 3: Build and run your application.

Note: You can also use a URL parameter instead of a constant string to initialize a filter value. For example, if the URL parameter is called CustomerID, then you can

C#:

    wc.iAND(OrdersTable.CustomerID, BaseFilter.ComparisonOperator.EqualsTo,
        this.Page.Request.QueryString["CustomerID"]);

Visual Basic .NET:

    wc.iAND(OrdersTable.CustomerID, BaseFilter.ComparisonOperator.EqualsTo, _         Me.Page.Request.QueryString(“CustomerID”))

About the Author
Anh Trinh
Software Engineer, Iron Speed, Inc.

Anh is a software engineer at Iron Speed, Inc. He enjoys developing applications with Iron Speed Designer and Microsoft .NET technology.



  Privacy Statement