Sometimes programmer need to create tables’ programmatically in their application. In ASP.Net we find a flexibility of adding HTML at runtime on the basis of requirement. The simple thing you need to do is add table to your aspx page and set the two properties of a table that are ID of a table and runat attribute. Now your Table is assessable to you at code behind.
To begin with this, create new ASP.Net 2.0 application and in code behind set language to C#. Now add a table and set it two attributes ID and runat. Now on the page load event add the following code:
protected void Page_Load(object sender, EventArgs e)
{
//Instantiate Row Object and Cell Object
HtmlTableRow htRow = new HtmlTableRow();
HtmlTableCell htCell = new HtmlTableCell();
//Set the sytle of a table
table1.BgColor = "#FCDDFE";
table1.Align = "left";
table1.Border = 1;
table1.Attributes["Style"] = "font-family:Verdana;font-size:11px;font-style:italic;";
//Add Text to cell and add Cell to Row
htCell.InnerText = "This is Implementation of HTML Control";
htRow.Cells.Add(htCell);
table1.Rows.Add(htRow);
//Add Second Row in a Table
htRow = new HtmlTableRow();
htCell = new HtmlTableCell();
TextBox txttext = new TextBox();
txttext.Visible = true;
txttext.Text = "Inside Cell";
htCell.Controls.Add(txttext);
}
In above you will see that first I have created the two new instance of HtmlTableRow and HtmlTableCell. Now set the properties of a table and add the rows and cells to the table. Here you will also see that in code you can also set the style sheet attribute and set its properties. Here you will also see that I have added TextBox control. So you can add any type of controls and text within cell.