Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Controls - Table Web server control in ASP.NET 2.0 (C#)
Table Web server control in ASP.NET 2.0 (C#)


ASP.NET Controls Tutorial

The Table Web server control allows you to create server-programmable tables on ASP.NET pages. This tutorial will show you how to use Table Web server control in ASP.NET 2.0 and C#.

The Table Web server control allows you to create server-programmable tables on ASP.NET pages. The TableRow and TableCell Web server controls provide a way to display the actual content for the Table control. The Table control builds up a table by adding TableRows to the Rows collection of the table, and TableCells to the Cells collection of the row. You can add content to a table cell by adding controls to the Controls collection of the cell.

protected void Page_Load(object sender, EventArgs e)
{
// Generate rows and cells

int numrows = int.Parse(DropDown1.SelectedItem.Value);
int numcells = int.Parse(DropDown2.SelectedItem.Value);

for (int j=0; j<numrows; j++)
{
TableRow r = new TableRow();

for (int i=0; i<numcells; i++)
{
TableCell c = new TableCell();
c.Controls.Add(new LiteralControl("row " + j.ToString() + ", cell " + i.ToString()));
r.Cells.Add(c);
}

Table1.Rows.Add(r);
}
}

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

The front end .aspx page looks something like this:

<body>

<form id="Form1" runat=server>
<fieldset>

<legend>Table Example</legend>
<asp:Table id="Table1" Font-Names="Verdana" Font-Size="8pt" CellPadding=5 CellSpacing=0 BorderColor="black" BorderWidth="1" Gridlines="Both" runat="server"/>

<p>
Table rows:
<asp:DropDownList id=DropDown1 runat="server">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
</asp:DropDownList>

<br>
Table cells:  <asp:DropDownList id=DropDown2 runat="server">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
</asp:DropDownList>

<p>
<asp:button ID="Button1" Text="Generate Table" runat=server/>
</fieldset>
</form>

</body>

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

The flow for the code behind page is as follows

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Generate rows and cells

int numrows = int.Parse(DropDown1.SelectedItem.Value);
int numcells = int.Parse(DropDown2.SelectedItem.Value);

for (int j=0; j<numrows; j++)
{
TableRow r = new TableRow();

for (int i=0; i<numcells; i++)
{
TableCell c = new TableCell();
c.Controls.Add(new LiteralControl("row " + j.ToString() + ", cell " + i.ToString()));
r.Cells.Add(c);
}

Table1.Rows.Add(r);
}
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C#.NET 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks