Modify Search Field Behavior

When a page containing a table control is loaded, it displays the first ten records in the table. This may be changed if you do not want users to see unrelated records.
- Mahalakshmi Ramachandran, Software Engineer, Iron Speed, Inc.

May 3, 2006
Iron Speed Designer V3.2

Procedure
When a page containing a table control is loaded, it displays the first ten records in the table. This may be changed if you do not want users to see unrelated records. In fact, you may want no records to be displayed if the search field is empty; records should be displayed only when the user enters something into the search field and clicks 'Go'.

To force the user to enter a value in the search field before displaying results in the table control, you can use the Code Customization Wizard example ‘Hide table control until searched’. This customization retrieves records based upon information entered into the search field. Unlike a normal table control that displays results when first displayed on a page, the results are displayed only after the user enters a term into the search field and clicks "Go".

This customization defines a "Load" handler in the "Safe" class for the Show Table object. In the handler, a filter object is defined that initially finds no records until a search term is entered. The filter is associated with the table's data reader and is invoked only during the "non-postback" phase to ensure it only happens the first time a page is loaded. Then, the user's actual input controls the display during subsequent postbacks.

C#:

public OrdersTableControl() {
    // The following line will be inserted inside the
    // constructor for page class.
    this.Load += new System.EventHandler(TableControl_Load);
}
 
/// <summary>
/// This method shows records of a table only on PostBack.
/// </summary>
/// <param name="sender">The object that raised the load event.</param>
/// <param name="e">The object that contains the event data of the load event.</param>
private void TableControl_Load(object sender, System.EventArgs e)
{
    if (!(this.Page.IsPostBack))
    {
            // Add a bogus filter to the table before search.
            // The WHERE clause defines the bogus filter.
            string whereStr = "1=2";
            this.AddFilter("myFilter", whereStr);
    }
    else if ((this.GetFilter("myFilter"))!= null)
    {
            // Remove the bogus filter from the table on postback and after
            this.RemoveFilter("myFilter");
            this.IsDataCurrent = false;
    }
}

Visual Basic .NET

''' Shows records of a table only on postback.
''' <param name="sender">The object that raised the load event.</param>
''' <param name="e">The object that contains the event data of the load event.</param>
Private Sub TableControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not Me.Page.IsPostBack Then
        ' Add a bogus filter to the table before search.
        ' The WHERE clause defines the bogus filter
        Dim whereStr As String = "1=2"
        Me.AddFilter("myFilter", whereStr)
    ElseIf (Not IsNothing(Me.GetFilter("myFilter"))) Then
        ' Remove the bogus filter from the table on
        ' postback and after search button is clicked.
        Me.RemoveFilter("myFilter")
        Me.IsDataCurrent = false
    End If
End Sub

To make sure no records are displayed until the user enters text into the search field and clicks ‘Go’, enable the ‘required’ field validation for the search control.

Step 1: In the Design tab in Iron Speed Designer, locate the 'Go' button for the search control. Double-click on this button to display the Properties dialog. In the Attributes tab, set the button's ‘CausesValidation’ attribute to ‘True’.

Pass-Through Attribute Value Type
Button- CausesValidation True HTML Encoded

Step 2: In the HTML layout page, add a RequiredFieldValidator control for the search area text box:

<asp:RequiredFieldValidator runat="server" id="OrdersSearchRequiredFieldValidator"
ControlToValidate="OrdersSearchAreaTextBox" ErrorMessage="A value for search is required."
Enabled="True"/>

The RequiredFieldValidator is a .NET control that ensures a value is entered for the field. Be sure to specify values for the ‘control to validate’ and ‘enabled’ attributes.

You can look up the ID of the search area text box from the generated ASPX page. For example, this tag in the generated ASPX page contains the text box control’s name.

<BaseClasses:SearchTextControl runat="server" id="OrdersSearchArea"
TextControl="OrdersSearchAreaTextBox" CaseSensitive="False" Consumers="OrdersTableControl"
Fields="OrderName" FilterOperator="Contains">
</BaseClasses:SearchTextControl>

This approach is the JavaScript alternative to handle the search box behavior.

Step 3: Build and run your application.

About the Author
Mahalakshmi Ramachandran
Software Engineer, Iron Speed, Inc.

Maha has experience developing Web Applications using the .NET technology and has been with Iron Speed since 2005.

Maha holds an M.S. degree in Computer Engineering from San Jose State University and a B.E. degree in Computer Science and Engineering from India.



  Privacy Statement