• home
  • Email: trainings@synapseindia.careers

Blogs

back

Working with ASP.NET Table Control Part – III

Apr 04, 2016

Asp.net table server control display a table similar to html table on a web page. It is a .net web server control so we can program it in server code such as add, remove table row, cell pro-grammatically. TableRow and TableCell classes allows us to display content for the table. Table server control display tabular data and we can format table server control using it's built in methods and properties. The major difference between html table and asp.net table server control is that we can manipulate table server control using an object model. Generally we should use table server control when we intend to add remove rows and cells (columns) pro-grammatically at run time. Take an example below, where we have added one button and one table control. On click of button , we have added two tables.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Table ID="Table1" runat="server">
</asp:Table>
</div>
</form>
</body>
</html>
See the code for .cs file below :
using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Table1.GridLines = GridLines.Both;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        insert("Employee Name ", "Employee Salary ", "Employee Mobile");
        insert("Arvind","18000","9874561231");
        insert("Shrey", "18000", "9874561231");
        insert("Deepak", "18000", "9874561231");
        insert("Piyush", "18000", "9874561231");

        numerictable(10, 10);
    }
    void insert(string a, string b , string c)
    {
        TableRow tr = new TableRow();
        TableCell cell1 = new TableCell();
        TableCell cell2 = new TableCell();
        TableCell cell3 = new TableCell();
        cell1.Text = a;
        cell2.Text = b;
        cell3.Text = c;
        tr.Cells.Add(cell1);
        tr.Cells.Add(cell2);
        tr.Cells.Add(cell3);
        Table1.Rows.Add(tr);

        
    }

    void numerictable(int rows, int columns)
    {
       Random rn = new Random();
        for (int r = 0; r < rows; r++)
        {
            TableRow tr = new TableRow();
            for (int c = 0; c < columns; c++)
            {
                TableCell cell = new TableCell();

                cell.Text = rn.Next(1, 100).ToString();
                tr.Cells.Add(cell);
            }
            Table1.Rows.Add(tr);
        }
    }
}

ASP.NET Server control provides various properties and methods to add cells and rows pro-grammatically. SynapseIndia has been working on various technologies required for developing web applications. Out of various SynapseIndia Technologies, .NET and PHP are majorly used by SynapseIndia.

SynapseIndia (CEO: Shamit Khemka)