I am currently upgrading a major MVS project and I am converting all the static GridViews to dynamically built HTML tables with Bootstrap CSS. The tables are created and look/function great.
However, I cannot seem to figure out how to call the table and iterate through from the code-behind to change the background color of cells based on the text they contain.
This function creates the table:
public static string ConvertDataTableToHTML(DataTable dt)
{
string html = "<table id=\"tbl_1\" runat=\"server\" class=\"table table-hover table-condensed\">";
//add header row
html += "<thead><tr>";
for (int i = 0; i < dt.Columns.Count; i++)
html += "<th style=\"background-color: #800000; color: #FFFFFF; font-weight: bold\">" + dt.Columns[i].ColumnName + "</th>";
html += "</thead></tr>";
//add rows
for (int i = 0; i < dt.Rows.Count; i++)
{
html += "<tr>";
for (int j = 0; j < dt.Columns.Count; j++)
html += "<td>" + dt.Rows[i][j].ToString() + "</td>";
html += "</tr>";
}
html += "</table>";
return html;
}
The above function is creating a table with an id of "tbl_1".
Then I have this code that calls the function, assigns the SQL data to the dt, sets some attributes of the dt and then assigns the dt to the dynamically created table and tries to loop through the cells and change their color - but nothing happens....
OracleDataReader dtReader;
objCmd = new OracleCommand(varname1, objConn);
dtReader = objCmd.ExecuteReader();
if (dtReader.HasRows == true)
{
DataTable dt1 = new DataTable();
dt1.Load(dtReader);
dt1.Columns[2].ReadOnly = false;
dt1.Columns[2].MaxLength = 200;
dt1.Columns[3].ReadOnly = false;
dt1.Columns[3].MaxLength = 200;
dt1.Columns[4].ReadOnly = false;
dt1.Columns[4].MaxLength = 200;
dt1.Columns[5].ReadOnly = false;
dt1.Columns[5].MaxLength = 200;
SG_COUNT_STATUS4(dt1);
myTBL_1.InnerHtml = ConvertDataTableToHTML(dt1);
System.Web.UI.WebControls.Table dynamicTable = (System.Web.UI.WebControls.Table)FindControl("tbl_1");
if (dynamicTable != null)
{
foreach (System.Web.UI.WebControls.TableRow row in dynamicTable.Rows)
{
foreach (System.Web.UI.WebControls.TableCell cell in row.Cells)
{
string cellText = cell.Text.ToLower();
if (cellText != "not run") { cell.BackColor = System.Drawing.Color.Red; }
}
}
}
}
It appears that the code is not actually setting a reference to table. If I remove the if (dynamicTable != null) trigger, it errors out on the next line saying
Object reference not set to an instance of an object.
How do I loop through the dynamically created table and change the background color based on the text inside the cell?
i am not sure what you mean by "there isn't even a table object". there is. it fills with data and displays beautifully. so, its there...I didn't say you didn't have a HTML table. I said you didn't have a server-side C# object which represents that in the code-behind, and which you can use to loop through and modify its contents. All you have is the plain pre-prepared HTML string which you generated in yourConvertDataTableToHTMLmethod and dumped directly into another div element. "FindControl" is looking for that server-side object, and can't find it because it's not there.I just need to know how to access it from the code-behind...and, we're back to the start. This is what GridViews are for - to give you structured, programmatic ways to render, control and populate HTML tables from the code-behind. Use the tools that the framework gave you, instead of abandoning them because of an apparent misconception that you wouldn't be able to change the final rendered appearance of the table they generate. So yeah...the "why" does matter. This question is a classic X-Y Problem as far as I can see.