Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Create a table with the Table control  (Read 756 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« on: July 15, 2009, 03:35:34 PM »

You can easily create tables using the Table control in .NET 2.0
The code below generates a 3x6 column table, i must say it took me some time to figure out how to use the Table control the "good" way but it's definitly better than hardcoding the tables in the .aspx page. Flexibility ftw.

Code
GeSHi (csharp):
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. public partial class TableTest : System.Web.UI.Page
  13. {
  14.    protected void Page_Load(object sender, EventArgs e)
  15.    {
  16.        // Generate rows and cells.          
  17.        Table table1 = new Table();
  18.        table1.Attributes.Add("width", "100%");
  19.        table1.Attributes.Add("border", "1");
  20.        table1.BorderWidth = 1;
  21.  
  22.        TableRow hearderrow = new TableRow();
  23.        hearderrow.CssClass = "tr";
  24.  
  25.        TableCell cell1 = new TableCell();
  26.        TableCell cell2 = new TableCell();
  27.        TableCell cell3 = new TableCell();
  28.        TableCell cell4 = new TableCell();
  29.        TableCell cell5 = new TableCell();
  30.        TableCell cell6 = new TableCell();
  31.  
  32.        cell1.Text = "cell1:";
  33.        cell2.Text = "cell2:";
  34.        cell3.Text = "cell3:";
  35.        cell4.Text = "cell4:";
  36.        cell5.Text = "cell5:";
  37.        cell6.Text = "cell6:";
  38.  
  39.        hearderrow.Cells.Add(cell1);
  40.        hearderrow.Cells.Add(cell2);
  41.        hearderrow.Cells.Add(cell3);
  42.        hearderrow.Cells.Add(cell4);
  43.        hearderrow.Cells.Add(cell5);
  44.        hearderrow.Cells.Add(cell6);
  45.  
  46.        //Add header to a table
  47.        table1.Rows.Add(hearderrow);
  48.  
  49.        //Add content to a table
  50.        for (int i = 0; i < 3; i++)
  51.        {
  52.            TableRow row = new TableRow();
  53.            int amountOfCell = 6;
  54.            TableCell[] cell = new TableCell[amountOfCell];
  55.  
  56.            for (int j = 0; j < amountOfCell; j++)
  57.            {
  58.                cell[j] = new TableCell();
  59.                if (j == 0)
  60.                {
  61.                    cell[j].Text = "" + DateTime.Now.ToString();
  62.                    row.Cells.Add(cell[j]);
  63.                }
  64.  
  65.                else
  66.                {      
  67.                   cell[j].Text = "cell1" + j;
  68.                    row.Cells.Add(cell[j]);
  69.                }
  70.            }
  71.  
  72.            table1.Rows.Add(row);
  73.  
  74.        }
  75.        PlaceHolder1.Controls.Add(table1);
  76.    }
  77. }
  78.  
Created by GeSHI 1.0.7.20
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here