Sponsored Ad

Thursday, July 15, 2010

How Dynamically Add Rows into ASP.NET Table using C#

This sample will help to add rows and cells into a asp.net table using C#.

You need to first create a row and then create a cell assign the properties of cell and then add the cell to row and then row to table.

 

How Dynamically Add Rows into ASP.NET Table using C#

 

How Dynamically Add Rows into ASP.NET Table using C# Example:

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        TableRow tr = new TableRow();
        TableCell tc = new TableCell();
        tc.Text = "PetsMantra.com";
        tr.Cells.Add(tc);
        TableCell tc2 = new TableCell();
        tc2.Text = "Join2Green.com";
        tr.Cells.Add(tc2);
        tblSample.Rows.Add(tr);
    }
</script>

<html>
<head>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        <asp:table ID="tblSample" runat="server"></asp:table>
        </div>
    </form>
</body>
</html>

1 comment:

  1. I'm trying to create rows by clicking a button. It's web based application in C#, where i'm using HtmlTableRow to create new row, every time user clicks a button. But problem is after one row, no new row is creating. I'm giving new ID of each row though variable name remains same.

    string htRw = "htRow" + Session["RowCount"].ToString();
    htRow1 = new HtmlTableRow();
    htRow1.ID = htRw;
    then incrementing Session["RowCount"] by 1.

    but new row is just replacing first row.

    ReplyDelete

Sponsored Ad

Development Updates