Override the DataBind Method on a TableControl Class

"Instead of writing the ASP.NET data grid code from scratch, you can use an Iron Speed Designer-generated table control to populate your ASP.NET data grid, saving you a lot of programming."
- Anh Trinh, Software Engineer, Iron Speed, Inc.

January 17, 2007
Iron Speed Designer V4.1

Override the DataBind Method on a TableControl Class
In some situations it may be desirable to use an ASP.NET data grid in your application instead of a table control generated by Iron Speed Designer, for example:
  • Existing data grid. Your application may have a page that already has an ASP.NET data grid, and you may wish to use the existing ASP.NET data grid instead of adding an Iron Speed Designer-generated table control.
  • Simplicity. You might find it easier to work with an ASP.NET data grid instead of an Iron Speed Designer-generated table control.
  • Familiarity. You may already be familiar with the ASP.NET data grid and don’t have time to learn how to customize code for a new table control.
However, instead of writing the ASP.NET data grid code from scratch, you can use an Iron Speed Designer-generated table control to populate your ASP.NET data grid, saving you a lot of programming. Since the ASP.NET data grid is populated using the Iron Speed Designer-generated table control, features such as filtering, search and pagination are automatically implemented.
Procedure
Step 1: Create an application in Iron Speed Designer using a database table, such as the Orders table in Northwind.

Step 2: In the HTML layout page file for ShowOrdersTablePage, delete all content between the

<!-- Table View Area -->

...and the...

<!-- Totals Area -->

...tags.

Step 3: Add the following content to replace the previous content:

<!--Table View Area -->
<tr>
  <td class="tableRowsEdge">
    <GEN:ITEMTEMPLATE Name="OrdersTableItem" ></GEN:ITEMTEMPLATE>
      <asp:datagrid id="myDataGrid" runat="server">
 
      <HeaderStyle BackColor = "#336699" ForeColor = "#ffffff" Font-Bold = "true" />
 
      <AlternatingItemStyle BackColor = "#eeeeee" />
 
    </asp:datagrid>
  </td>
</tr>
<!-- Totals Area -->

Step 4: Override the DataBind method in the TableControl class of the ShowOrdersTablePage.aspx web page.

For .NET Framework 1.1, add your code in the OrdersTableControl class, located in:

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

For .NET Framework 2.0 / 3.0, add your code in OrdersTableControl class, located in:

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

C#:

public override void DataBind()
{
    base.DataBind();
    if (this.DataSource == null)
    {
        return;
    }
    BindPaginationControls();
    System.Web.UI.WebControls.DataGrid grid = (System.Web.UI.WebControls.DataGrid)(this.FindControl("myDataGrid"));
    this.PopulateCustomerIDFilter(MiscUtils.GetSelectedValue(this.CustomerIDFilter,
        this.GetFromSession(this.CustomerIDFilter)), 500);
    this.PopulateEmployeeIDFilter(MiscUtils.GetSelectedValue(this.EmployeeIDFilter,
        this.GetFromSession(this.EmployeeIDFilter)), 500);
    System.Data.DataTable table = OrdersTable.Instance.CreateDataTable(this.DataSource);
    grid.DataSource = table;
    grid.DataBind();
}

Visual Basic .NET:

Public Overrides Sub DataBind()
    MyBase.DataBind
    If (Me.DataSource Is Nothing) Then
        Return
    End If
    BindPaginationControls
    Dim grid As System.Web.UI.WebControls.DataGrid =
        CType(Me.FindControl("myDataGrid"),System.Web.UI.WebControls.DataGrid)
    Me.PopulateCustomerIDFilter(MiscUtils.GetSelectedValue(Me.CustomerIDFilter,
        Me.GetFromSession(Me.CustomerIDFilter)), 500)
    Me.PopulateEmployeeIDFilter(MiscUtils.GetSelectedValue(Me.EmployeeIDFilter,
        Me.GetFromSession(Me.EmployeeIDFilter)), 500)
    Dim table As System.Data.DataTable = OrdersTable.Instance.CreateDataTable(Me.DataSource)
    grid.DataSource = table
    grid.DataBind
End Sub

Step 5: Build and run your application.

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