Ramco Ramco

Ramco Ramco

  • 425
  • 3.4k
  • 490.6k

Dynamic Html Table

Sep 3 2024 4:08 AM

Hi

  Can the below code can be done dynamically. Tables & no of fields van be different. Now i have to write in every file

StringBuilder htmlTable = new StringBuilder();
con.ConnectionString = ConfigurationManager.ConnectionStrings["cnn_Live"].ConnectionString; ;
SqlCommand cmd = new SqlCommand("select * from EmployeeCategory order by Category", con);
cmd.CommandType = CommandType.Text;
if (con.State == ConnectionState.Closed)
{
    con.Open();
}
SqlDataAdapter sda = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
    Int32 Count = 1;
    htmlTable.Append("<table class='table table-bordered table-hover datatable-highlight' id='tbldata'>");
    htmlTable.Append("<thead><tr style='background-color:green;color:white; text-align: center;'><th>Sr.</th><th>Name</th><th>Short Name</th><th>Status</th><th class='text-center nosort'>Action</th><th style='display:none'</th><th style='display:none'</th></tr></thead>");
    htmlTable.Append("<tbody>");
    foreach (DataRow row in dt.Rows)
    {
        String Status = "Blocked";
        if (Convert.ToBoolean(row["status"]))
        {
            Status = "Active";
        }

        htmlTable.Append("<tr>");
        htmlTable.Append("<td>" + Count.ToString() + "</td>");
        htmlTable.Append("<td>" + row["category"] + "</td>");
        htmlTable.Append("<td>" + row["shortname"] + "</td>");
        if (Status == "Active")
            htmlTable.Append("<td class='text-center'><a  id='btnStatus' class='badge bg-primary'>" + Status + "</a></td>");
        else
            htmlTable.Append("<td class='text-center'><a  id='btnStatus' class='badge bg-danger'>" + Status + "</a></td>");

        htmlTable.Append("<td class='text-center'>");
        htmlTable.Append("<a id='btnEdit' class='list-icons-item text-primary-600' data-toggle='modal' data-backdrop='static' data-keyboard='false' data-target='#modal_form_horizontal' onclick='BindData(this);'><i class='icon-pencil7 mr-1'></i></a>");

        string deleteAttributes = Status == "Blocked" ? "style='pointer-events: none; opacity: 0.5;' onclick='return false;'" : "data-toggle='modal' data-target='#modal_Delete' onclick='BindData(this);'";
        htmlTable.Append($"<a id='btnDel' class='list-icons-item text-danger-600' {deleteAttributes}><i class='icon-bin mr-1'></i></a>");

        htmlTable.Append("</td>");
        

        htmlTable.Append("<td style='display:none'>" + row["docentry"] + "</td>");
        htmlTable.Append("<td style='display:none'>" + row["status"] + "</td>");
        htmlTable.Append("</tr>");
        Count++;
    }
    htmlTable.Append("</tbody>");
    htmlTable.Append("</table>");
    PlaceHolderTable.Controls.Add(new Literal { Text = htmlTable.ToString() });

Thanks


Answers (3)