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.
- Anh Trinh, Software Engineer, Iron Speed, Inc.

October 24, 2006
Iron Speed Designer V4.0

Procedure
By default, when a page containing a table control is loaded, it displays the first ten records in the table. This may be undesirable 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".

The following code customization example is based on the Orders table in the Northwind database. Add the code customization into the OrdersTableControl class:

.NET Framework 1.1:

<App Folder>Orders\ShowOrdersTablePage.Controls.cs or .vb

.NET Framework 2.0:

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

C#:

public override WhereClause CreateWhereClause()
{
    WhereClause wc = base.CreateWhereClause();
 
    // If base.CreateWhereClause() returns nothing,
    // create a new instance of WhereClause.
    if (wc == null)
    {
        wc = new WhereClause();
    }
 
    // If no value is entered in the search area hide the table control
    if (!MiscUtils.IsValueSelected(this.OrdersSearchArea) && !(this.InSession
    (this.OrdersSearchArea)))
    {
        // Set RunQuery = False
        wc.RunQuery = false;
    }
    return wc;
}

Visual Basic .NET:

Public Overrides Function CreateWhereClause() As WhereClause
     Dim wc As WhereClause = MyBase.CreateWhereClause()
     If (IsNothing(wc)) Then
          wc = New WhereClause
     End If
     ' If no value is entered in the search area hide the table control
     If Not IsValueSelected(Me.${Table}SearchArea) AndAlso _ Not Me.InSession(Me.${Table}
     SearchArea) Then
          ' Set RunQuery = False
          wc.RunQuery = False
     End If
     Return wc
End Function

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

Step 1: In the Design tab in Iron Speed Designer, locate the 'Go' button for the search control. Double-click the ‘Go’ 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 search value 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 ‘ControlToValidate’ and ‘Enabled’ attributes.

Step 3: Build and run your application.

Overriding the Button Click Handler Method
A different approach is to override the OrdersSearchButton_Click() method to check if the SearchTextArea is empty. If it is, then display a pop-up message. Otherwise, execute the base OrdersSearchButton_Click(). By adding this code customization, you don’t have to change the Button attributes and add any code to your HTML page. Add this code customization into the OrdersTableControl class:

.NET Framework 1.1:

<App Folder>\Orders\ShowOrdersTablePage.Controls.cs or .vb

.NET Framework 2.0:

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

C#:

public override void OrdersSearchButton_Click(object sender, EventArgs args)
{
    If (this.OrdersSearchArea.Text == "")
    {
        BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(this, "UNIQUE_SCRIPTKEY", "Search text
        field is empty!");
    }
    else
    {
        base.OrdersSearchButton_Click(sender, args);
        // Or more code customization can be added
    }
}

Visual Basic .NET:

Public Overrides Sub OrdersSearchButton_Click(ByVal sender As Object, ByVal args As EventArgs)
    If ((Me.OrdersSearchArea.Text = ""))
        BaseClasses.Utils.MiscUtils.RegisterJScriptAlert(Me, "UNIQUE_SCRIPTKEY", "Search text
        field is empty!")
    End If
    MyBase.OrdersSearchButton_Click(sender, args)
    ' Or more code customization can be added
End Sub

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