Override the DataBind Method on a Page Class


"Discover how to add an ASP.NET data grid to the bottom of an Iron Speed Designer-generated table panel."
- Anh Trinh, Software Engineer, Iron Speed, Inc.

January 10, 2007
Iron Speed Designer V4.1

Introduction

In some situations it may be desirable to use an ASP.NET data grid in your application along with Iron Speed Designer table panel. This example adds an ASP.NET data grid at the bottom of a Show Table page, below an Iron Speed Designer-generated table panel.

Procedure

Step 1: Open the HTML layout file of the ASPX page where a DataGrid is to be added.

Step 2: Locate the following HTML code in the HTML layout file.

<tr>
    <td>
    </td>
    <td>
        < GEN:FOOTER FILE="../Header & Footer/Footer.html" NAME="Footer"></GEN:FOOTER>
    </td>
</tr>

Step 3: Add the following HTML code just before the HTML code above.

<tr>
    <asp:datagrid id="myDataGrid" OnItemDataBound= "MyItemDataBound" runat="server">
        <HeaderStyle BackColor = "#336699" ForeColor = "#ffffff" Font-Bold = "true" />
        <AlternatingItemStyle BackColor = "#eeeeee" />
    </asp:datagrid>
</tr>

Step 4: Override the DataBind() method in the Page class, located in:

.NET Framework 1.1:

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

.NET Framework 2.0:

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

C#:

public override void DataBind()
{
    base.DataBind();
    if (!this.Page.IsPostBack)
    {
        System.Web.UI.WebControls.DataGrid myDataGrid;
        myDataGrid = ((System.Web.UI.WebControls.DataGrid)(this.FindControlRecursively
            ("myDataGrid")));
        if (!(myDataGrid == null))
        {
            string whereStr = null;
            BaseClasses.Data.OrderBy ob = null;
            int pageIndex = 0;
            int pageSize = 1000;
            myDataGrid.DataSource = OrdersTable.GetDataTable(whereStr, ob, pageIndex,
            pageSize);
            myDataGrid.DataBind();
        }
    }
}
public void MyItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
    if (e.Item.ItemType == System.Web.UI.WebControls.ListItemType.Item ||
        e.Item.ItemType == System.Web.UI.WebControls.ListItemType.AlternatingItem)
    {
        for(int i = 1; i < (e.Item.Cells.Count); i++)
        {
            e.Item.Cells[i].Style["cursor"] = "hand";
        }
    }
}

Visual Basic .NET:

Public Overrides Sub DataBind()
    MyBase.DataBind()
    If (Not Me.Page.IsPostBack) Then
        Dim myDataGrid As System.Web.UI.WebControls.DataGrid
        myDataGrid = CType(Me.FindControlRecursively("myDataGrid"),
            System.Web.UI.WebControls.DataGrid)
 
        If (Not myDataGrid Is Nothing) Then
            Dim whereStr As String = Nothing
            Dim ob As BaseClasses.Data.OrderBy = Nothing
            Dim pageIndex As Integer = 0
            Dim pageSize As Integer = 1000
 
            myDataGrid.DataSource = OrdersTable.GetDataTable(whereStr, ob, pageIndex,
            pageSize)
            myDataGrid.DataBind()
        End If
    End If
End Sub
 
Public Sub MyItemDataBound(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
    If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
        ListItemType.AlternatingItem Then
        Dim i As Integer = 0
 
        While i < (e.Item.Cells.Count)
            e.Item.Cells(i).Style("cursor") = "hand"
            System.Math.Min(System.Threading.Interlocked.Increment(i),i-1)
        End While
    End If
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