ASP.NET Table Server Control Sample | How to use Table Control?


TableServerControl.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TableServerControl.aspx.cs" Inherits="WebDemo01.TableServerControl" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title>Table control Example</title>
</head>

<body>
    <form id="form1" runat="server">
        <div>
            <asp:Table ID="tabEmpDetails" runat="server"                               BorderStyle="Solid" BorderWidth="2px" 
              GridLines="Both" Width="800px">
            </asp:Table>
        </div>
    </form>
</body>
</html>

TableServerControl.aspx.cs

using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

namespace WebDemo01
{
    public partial class TableServerControl : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ShowEmployeeRecords();
        }
        protected void ShowEmployeeRecords()
        {
            using (SqlConnection con = new SqlConnection
                     (ConfigurationManager
                       .ConnectionStrings["MyDBConStr"].ConnectionString))
            {
                con.Open();

                TableRow tr;
                TableCell tc;

                using (SqlCommand cmd 
                 = new SqlCommand("select * from tabEmployee", con))
                {
                    SqlDataReader dr = cmd.ExecuteReader();
                    tr = new TableRow();

                    tc = new TableCell();
                    tc.Text = "Emp.Number";
                    tc.Font.Bold = true;
                    tr.Cells.Add(tc);

                    tc = new TableCell();
                    tc.Text = "Name";
                    tc.Font.Bold = true;
                    tr.Cells.Add(tc);

                    tc = new TableCell();
                    tc.Text = "Department";
                    tc.Font.Bold = true;
                    tr.Cells.Add(tc);

                    tc = new TableCell();
                    tc.Text = "Salary";
                    tc.Font.Bold = true;
                    tr.Cells.Add(tc);

                    tc = new TableCell();
                    tc.Text = "Bonus";
                    tc.Font.Bold = true;
                    tr.Cells.Add(tc);

                    tc = new TableCell();
                    tc.Text = "Net salary";
                    tc.Font.Bold = true;
                    tr.Cells.Add(tc);

                    tabEmpDetails.Rows.Add(tr);

                    int sal;
                    double bonus = 0, netSal = 0, totNetSalary = 0;

                    while (dr.Read())
                    {
                        tr = new TableRow();

                        tc = new TableCell();
                        tc.Text = dr["EmpNo"].ToString();
                        tr.Cells.Add(tc);

                        tc = new TableCell();

                        if (dr["Gender"].ToString().Equals("Male"))
                            tc.Text = "Mr." + dr["EmpName"].ToString();
                        else if (dr["Gender"].ToString().Equals("Female"))
                            tc.Text = "Ms." + dr["EmpName"].ToString();
                        else
                            tc.Text = dr["EmpName"].ToString();

                        tr.Cells.Add(tc);

                        tc = new TableCell();
                        tc.Text = dr["Dept"].ToString();
                        tr.Cells.Add(tc);

                        tc = new TableCell();
                        sal = Convert.ToInt16(dr["Salary"]);
                        tc.Text = sal.ToString();
                        tc.HorizontalAlign = HorizontalAlign.Right;
                        tr.Cells.Add(tc);

                        bonus = sal * (10.0f / 100.0f);
                        netSal = sal + bonus;

                        tc = new TableCell();
                        tc.Text = Math.Round(
                                 (decimal)bonus, 2).ToString();
                        tc.HorizontalAlign = HorizontalAlign.Right;
                        tr.Cells.Add(tc);

                        tc = new TableCell();
                        tc.Text = Math.Round(
                                 (decimal)netSal, 2).ToString();
                        tc.HorizontalAlign = HorizontalAlign.Right;
                        tr.Cells.Add(tc);

                        totNetSalary = totNetSalary + netSal;

                        tabEmpDetails.Rows.Add(tr);
                    }
                    tr = new TableRow();

                    tc = new TableCell();
                    tc.Text = "Total Net Salary of all Employees";
                    tc.ColumnSpan = 5;
                    tc.Font.Bold = true;
                    tr.Cells.Add(tc);

                    tc = new TableCell();
                    tc.Text = Math.Round(
                             (decimal)totNetSalary, 2).ToString();
                    tc.HorizontalAlign = HorizontalAlign.Right;
                    tc.Font.Bold = true;
                    tr.Cells.Add(tc);

                    tabEmpDetails.Rows.Add(tr);
                }
            }
        }
    }
}

No comments:

Post a Comment