asp.net c# tutorials and examples code for web developers

Add TableRow and TableCell programmatically in asp.net Table

asp.net Table example: how to add TableRow and TableCell programmatically
AddTableRowAddTableCellExample.aspx
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_load(object sender, System.EventArgs e) {
        Label1.Text = "First Name";
        Label1.Font.Bold = true;
        Label1.ForeColor = System.Drawing.Color.DodgerBlue;
        Label2.Text = "Last Name";
        Label2.Font.Bold = true;
        Label2.ForeColor = System.Drawing.Color.DodgerBlue;
        Button1.Font.Bold = true;
        Button1.ForeColor = System.Drawing.Color.DodgerBlue;
    }
        
    protected void Button1_Click(object sender, System.EventArgs e) {
        TableRow TRow = new TableRow();
        
        TableCell FirstNameCell = new TableCell();
        FirstNameCell.Text = TextBox1.Text;
        TRow.Cells.Add(FirstNameCell);
        
        TableCell LastNameCell = new TableCell();
        LastNameCell.Text = TextBox2.Text;
        TRow.Cells.Add(LastNameCell);
        
        Table1.Rows.Add(TRow);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net Table example: how to add TableRow and TableCell programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">Add a Row and Cell</h2>
        <asp:Table ID="Table1" runat="server" BorderColor="DarkGreen" BorderWidth="1">
            <asp:TableHeaderRow ForeColor="SeaGreen">
                <asp:TableHeaderCell>First Name</asp:TableHeaderCell>
                <asp:TableHeaderCell>Last Name</asp:TableHeaderCell>
            </asp:TableHeaderRow>
            <asp:TableRow>
                <asp:TableCell>Jenny</asp:TableCell>
                <asp:TableCell>Jones</asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <br /><br />
        <asp:Label ID="Label1" runat="server" AssociatedControlID="TextBox1"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator 
             ID="RequiredFieldValidator1" 
             runat="server"
             ControlToValidate="TextBox1"
             Text="*"
             >
        </asp:RequiredFieldValidator>
        <br />
        <asp:Label ID="Label2" runat="server" AssociatedControlID="TextBox2"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator 
             ID="RequiredFieldValidator2" 
             runat="server"
             ControlToValidate="TextBox2"
             Text="*"
             >
        </asp:RequiredFieldValidator>

        <br />
        <asp:Button ID="Button1" runat="server" Text="Add Row and Cell" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>








Related asp.net examples