In ASP.Net 2.0 there are number of controls to display the data retrieved from the database in tabular form using Grid View Control, list view Control, Repeater. But in some cases when there is no option to custom these default controls. At that time a questions arises – how to generate dynamic tables? How to display the data according to requirement?
You can use the Literal Control to display the html tagged string generated by using VB/C# code in ASP.Net 2.0.
(Advance ASP.Net developers: See Alternate Example here using Panel Control to Create dynamic tables using HtmlControls HtmlTable)
To generate the table using string variables you can use the following C# code:
// create a string type variable to generate dynamic table
string dynTable="";
// start with table tag with following attributes
dynTable = "<table cellspacing=\"0\" cellpadding=\"2\" border=\"1\">";
// outer loop to generate table rows
{
//start table row
dynTable += "<tr>";
// inner loop to generate columns
// create column
// close td column tag
}
// close table row
dynTable += "</tr>";
// close the table tag
dynTable += "</table>";
Literal1.Text = dynTable;
Above C# code will build a string having table tag, tr as table row, td as table data/column. To display the data retrieved from database you can set the tRows < [No. of DataRows Retrieved] and tCols < [No. of DataColmns].
Output Result of above code:
Learn Also:
ASP.Net C# Database Connection
ASP.Net Connect to SQL Database
Currently rated 2.7 by 3 people
Tags: asp.net 2.0, dynamic table, c# code, free source code, literal control
10/11/2008 3:28:29 AM