Creating a table programmatically in ASP.NET

This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".

You can even create a table and add its cells and cell values programmatically. First, create a table using the <asp:Table> tag and then add rows to it(see Listing 7-27).

Listing 7-27. Creating a table's rows programmatically 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Creatingtableprogrammatically._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Table ID="table1" runat="server" Height="114" Width="439" BackColor=Aqua>
            <asp:TableRow>
            </asp:TableRow>
            <asp:TableRow>
            </asp:TableRow>
            <asp:TableRow>
            </asp:TableRow>
            <asp:TableRow>
            </asp:TableRow>
            <asp:TableRow>
            </asp:TableRow>
        </asp:Table>
    </div>
    </form>
</body>
</html>

Now you add cells and their values at run-time using the TableRow and TableCell class object (see Listing 7-28).

Listing 7-28. Adding table rows and cells programmatically 

            int rows = 3;
            int cols = 2;
            for (int j = 0; j < rows; j++)
            {
                TableRow r = new TableRow();
                for (int i = 0; i < cols; i++)
                {
                    TableCell c = new TableCell();
                    c.Controls.Add(new LiteralControl("row " + j.ToString() + ", cell " + i.ToString()));
                    r.Cells.Add(c);
                }
                Table1.Rows.Add(r);
            }

The example you saw in Listing 7-28 creates a table with three rows and two columns. You can also add rows and columns to a table programmatically. This program, which looks like Figure 7-58, adds rows and columns to a table at run-time based on the values entered in the text boxes. You create a Web application and add a control, two labels, and two text boxes to the page.

First, you create one <asp:Table> control to your .aspx page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Creatingtableprogrammatically._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Table ID="Table1" runat="server">
        </asp:Table>
        Rows:
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        Columns:
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Create Table" OnClick="Button1_Click" /><br />
    </div>
    </form>
</body>
</html>

Figure-7.58.jpg

Figure 7-58. Creating a table programmatically 

Next, add the code in Listing7-29 on the button-click handler.

Listing 7-29. Button-click handler create a table programmatically

        protected void Button1_Click(object sender, EventArgs e)
        {
            int rows = Convert.ToInt16(TextBox1.Text);
            int cols = Convert.ToInt16(TextBox2.Text);

            for (int j = 0; j < rows; j++)
            {
                TableRow r = new TableRow();
                for (int i = 0; i < cols; i++)
                {
                    TableCell c = new TableCell();
                    c.Controls.Add(new LiteralControl("row" + j.ToString() + ", cell " + i.ToString()));
                    r.Cells.Add(c);
                }
                Table1.Rows.Add(r);
            }
        }

Finally, run the application and enter the number of rows and the number of columns to the text boxes and then click the Create table button to create a table.

Conclusion

Hope this article would have helped you in understanding creating a table programmatically in ASP.NET. See other articles on the website also for further reference.

adobook.jpg
This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET.


Similar Articles