Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Add Rows and Cell to HTMLTable through Code

Author: DevASP Team
Download Source Code : 681_HTMLControl.zip

In this article I will try to explain you how you can add rows and cell to you HTMLTable programmatically.

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);

        htRow.Cells.Add(htCell);

        table1.Rows.Add(htRow);

 

}

 

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.

Article Comments
In my module i have put one table control from html. When the page loads i have inserted one row.in first row of the first cell i have put dropdown and in the second textbox and in third i have put radio button. when i click the button add rows i want to add the same row as above.

Posted on 11/2/2007 3:22:37 AM by muzzammil

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to Format Date in GridView Control

Disclaimer - Privacy
© 2002-2017 DevASP.net