2
Answers

Dynamic HTML from Code Behind

Matt Warren

Matt Warren

6y
144
1
Hello, I have inherited some C#.NET code that builds a dynamic dashboard on page load. The dynamic dashboard consists of a table and 2 rows - one row showing online devices, the other row showing offline devices. My issue is that if there are more than 5 devices on a given row, the row gets cluttered and looks terrible. I would like to break the line at 4 devices, but I am so confused by the logic. I have spent the last 48 hours swirling through this code and trying to find where to break the line and how - I am still lost. Please, any help would be very much appreciated. Please see a snapshot of the code below. Thank you so much in advance. -Jason
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (this.Session["UserID"] == null)  
  4.     {  
  5.         Response.Redirect("Login.aspx");  
  6.     }  
  7.         Response.AppendHeader("Refresh""8");  
  8.         HtmlMeta meta = new HtmlMeta();  
  9.         meta.HttpEquiv = "Content-Language";  
  10.         meta.Content = "en";  
  11.         MetaPlaceHolder.Controls.Add(meta);  
  12.         List<TableCell> OnCells = new List<TableCell>();  
  13.         List<TableCell> OffCells = new List<TableCell>();  
  14.   
  15.         DataTable dt = DatabaseObjects.UserDO.GetAllBoardID(Int32.Parse(this.Session["UserID"].ToString()));  
  16.   
  17.         foreach (DataRow r in dt.Rows)  
  18.         {  
  19.             string inner = r["DeviceType"].ToString();  
  20.   
  21.               
  22.             TableCell cell = new TableCell();  
  23.   
  24.             DataTable FonaDT = new DataTable();  
  25.             DataTable dt2 = new DataTable();  
  26.             DataTable fonaReturn = new DataTable();  
  27.             DataTable normalReturn = new DataTable();  
  28.             DateTime now = new DateTime();  
  29.   
  30.             if (DatabaseObjects.UserDO.IsFona(r["BoardID"].ToString()))  
  31.             {  
  32.                 FonaDT = DatabaseObjects.UserDO.GetFonaSensors(r["BoardID"].ToString());  
  33.             }  
  34.             else  
  35.             {  
  36.                 dt2 = DatabaseObjects.UserDO.GetSensorIDFromBoardID(r["BoardID"].ToString());  
  37.             }  
  38.             //cell.Text = "<label style=\"color:red; font-size:large;\">Device :</label><label style=\"color:#67BCDB; font-size:large;text-decoration:underline;\">" + inner + "</label>";  
  39.             //cell.BackColor = System.Drawing.Color.Gray;  
  40.             //cell.ColumnSpan = 30;  
  41.             if (FonaDT.Rows.Count > 0)  
  42.             {  
  43.                 //sensorRow.Cells.Add(cell);  
  44.                 int count = 0;  
  45.                 foreach (DataRow r2 in FonaDT.Rows)  
  46.                 {  
  47.                     if (count > 0)  
  48.                     {  
  49.                         cell = new TableCell();  
  50.                         //cell.Text += r2["sensor_name"].ToString();  
  51.                         cell.HorizontalAlign = HorizontalAlign.Center;  
  52.                         fonaReturn = DatabaseObjects.UserDO.GetDashboardData(r["BoardID"].ToString(), r2["sensor_name"].ToString());  
  53.                         if (fonaReturn.Rows.Count > 0)  
  54.                         {  
  55.                             now = DateTime.Now;  
  56.                             DateTime LastData = DateTime.Parse(fonaReturn.Rows[0]["ts_Datetime"].ToString());  
  57.                             TimeSpan diff = now.Subtract(LastData);  
  58.                             if (diff.TotalSeconds <= 120)  
  59.                             {  
  60.                                 //Its on  
  61.                                 cell.Text += "<div class=\"badgeOn\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";  
  62.                                 cell.Text += "</br>" + r2["sensor_name"].ToString();    
  63.                                 cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + fonaReturn.Rows[0]["value"].ToString() + "</label>";  
  64.                                 cell.Text += "</div>";  
  65.                                 OnCells.Add(cell);  
  66.                             }  
  67.                             else  
  68.                             {  
  69.                                 //its off  
  70.                                 cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";  
  71.                                 cell.Text += "</br>" + r2["sensor_name"].ToString();    
  72.                                 cell.Text += "</br> Last Online: " + LastData.ToString("MM/dd/yyyy hh:mm tt");  
  73.                                 cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + fonaReturn.Rows[0]["value"].ToString() + "</label>";  
  74.                                 cell.Text += "</div>";  
  75.                                 OffCells.Add(cell);  
  76.                             }  
  77.   
  78.                         }  
  79.                         else  
  80.                         {  
  81.                             cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";  
  82.                             cell.Text += "</br>" + r2["sensor_name"].ToString();    
  83.                             cell.Text +=  "</br> Last Online: " + " N/A"+"</br>";  
  84.                             cell.Text += "</br> <label style=\"color:red\">" + "Last Value: N/A" + "</label>";  
  85.                             OffCells.Add(cell);  
  86.   
  87.                         }  
  88.                         //cell.BorderStyle = BorderStyle.Solid;  
  89.                         //boardRow.Cells.Add(cell);  
  90.   
  91.                     }  
  92.                     else  
  93.                     {  
  94.                         cell = new TableCell();  
  95.                         //cell.Text += r2["sensor_name"].ToString() + "</br>";  
  96.                         cell.HorizontalAlign = HorizontalAlign.Center;  
  97.                         fonaReturn = DatabaseObjects.UserDO.GetDashboardData(r["BoardID"].ToString(), r2["sensor_name"].ToString());  
  98.                         if (fonaReturn.Rows.Count > 0)  
  99.                         {  
  100.                             now = DateTime.Now;  
  101.                             DateTime LastData = DateTime.Parse(normalReturn.Rows[0]["ts_Datetime"].ToString());  
  102.                             TimeSpan diff = now.Subtract(LastData);  
  103.                             if (diff.TotalSeconds <= 120)  
  104.                             {  
  105.                                 //Its on  
  106.                                 cell.Text += "<div class=\"badgeOn\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";  
  107.                                 cell.Text += "</br>" + r2["sensor_name"].ToString();    
  108.                                  
  109.                                 cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + fonaReturn.Rows[0]["value"].ToString() + "</label>";  
  110.                                 cell.Text += "</div>";  
  111.                                 OnCells.Add(cell);  
  112.                             }  
  113.                             else  
  114.                             {  
  115.                                 //its off  
  116.                                 cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";  
  117.                                 cell.Text += "</br>" + r2["sensor_name"].ToString();    
  118.                                 cell.Text += "</br> Last Online: " + LastData.ToString("MM/dd/yyyy hh:mm tt");  
  119.                                 cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + fonaReturn.Rows[0]["value"].ToString() + "</label>";  
  120.                                 cell.Text += "</div>";  
  121.                                 OffCells.Add(cell);  
  122.                             }  
  123.   
  124.                         }  
  125.                         else  
  126.                         {  
  127.                             cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";  
  128.                             cell.Text += "</br>" + r2["sensor_name"].ToString();  
  129.                             cell.Text += "</br> Last Online: " + " N/A" + "</br>";  
  130.                             cell.Text += "</br> <label style=\"color:red\">" + "Last Value: N/A" + "</label>";  
  131.                             cell.Text += "</div>";  
  132.                             OffCells.Add(cell);  
  133.                         }  
  134.                         //cell.BorderStyle = BorderStyle.Solid;  
  135.                         //boardRow.Cells.Add(cell);  
  136.   
  137.                     }  
  138.                     count++;  
  139.                 }  
  140.             }  
  141.             if (dt2.Rows.Count > 0)  
  142.             {  
  143.                 //sensorRow.Cells.Add(cell);  
  144.                 int count2 = 0;  
  145.                 foreach (DataRow r3 in dt2.Rows)  
  146.                 {  
  147.   
  148.                     if (count2 > 0)  
  149.                     {  
  150.                         cell = new TableCell();  
  151.                         //cell.Text += r3["SensorID"].ToString() + "</br>";  
  152.                         cell.HorizontalAlign = HorizontalAlign.Center;  
  153.                         normalReturn = DatabaseObjects.UserDO.GetDashboardData(r["BoardID"].ToString(), r3["SensorID"].ToString());  
  154.                         if (normalReturn.Rows.Count > 0)  
  155.                         {  
  156.                             now = DateTime.Now;  
  157.                             DateTime LastData = DateTime.Parse(normalReturn.Rows[0]["ts_Datetime"].ToString());  
  158.                             TimeSpan diff = now.Subtract(LastData);  
  159.                             if (diff.TotalSeconds <= 120)  
  160.                             {  
  161.                                 //Its on  
  162.                                 cell.Text += "<div class=\"badgeOn\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";  
  163.                                 cell.Text += "</br>" + r3["SensorID"].ToString();    
  164.                                   
  165.                                 cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + normalReturn.Rows[0]["value"].ToString() + "</label>";  
  166.                                 cell.Text += "</div>";  
  167.                                 OnCells.Add(cell);  
  168.                             }  
  169.                             else  
  170.                             {  
  171.                                 //its off - first most common  
  172.                                 cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";  
  173.                                 cell.Text += "</br>" + r3["SensorID"].ToString();    
  174.                                 cell.Text +=  "</br> Last Online: " + LastData.ToString("MM/dd/yyyy hh:mm tt");  
  175.                                 cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + normalReturn.Rows[0]["value"].ToString() + "</label>";  
  176.                                 cell.Text += "</div>";  
  177.                                 OffCells.Add(cell);  
  178.                             }  
  179.   
  180.   
  181.                         }  
  182.                         else  
  183.                         {  
  184.                             cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";  
  185.                             cell.Text += "</br>" + r3["SensorID"].ToString();  
  186.                             cell.Text += "</br> Last Online: " + " N/A" + "</br>";  
  187.                             cell.Text += "</br> <label style=\"color:red\">" + "Last Value: N/A" + "</label>";  
  188.                             cell.Text += "</div>";  
  189.                             OffCells.Add(cell);  
  190.                         }  
  191.                         //cell.BorderStyle = BorderStyle.Solid;  
  192.                         //boardRow.Cells.Add(cell);  
  193.   
  194.                     }  
  195.                     else  
  196.                     {  
  197.                         cell = new TableCell();  
  198.                         normalReturn = DatabaseObjects.UserDO.GetDashboardData(r["BoardID"].ToString(), r3["SensorID"].ToString());  
  199.                         //cell.Text += r3["SensorID"].ToString() + "</br>";  
  200.                         cell.HorizontalAlign = HorizontalAlign.Center;  
  201.                         if (normalReturn.Rows.Count > 0)  
  202.                         {  
  203.                             now = DateTime.Now;  
  204.                             DateTime LastData = DateTime.Parse(normalReturn.Rows[0]["ts_Datetime"].ToString());  
  205.   
  206.                             TimeSpan diff = now.Subtract(LastData);  
  207.                             if (diff.TotalSeconds <= 120)  
  208.                             {  
  209.                                 //Its on  
  210.                                 cell.Text += "<div class=\"badgeOn\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";  
  211.                                 cell.Text +="</br>"+ r3["SensorID"].ToString();   
  212.                                   
  213.                                 cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + normalReturn.Rows[0]["value"].ToString() + "</label>";  
  214.                                 cell.Text += "</div>";  
  215.                                 OnCells.Add(cell);  
  216.                             }  
  217.                             else  
  218.                             {  
  219.                                 //its off - second most common  
  220.                                 cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";  
  221.                                 cell.Text += "</br>" + r3["SensorID"].ToString();   
  222.                                 cell.Text +=  "</br> Last Online: " + LastData.ToString("MM/dd/yyyy hh:mm tt");  
  223.                                 cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + normalReturn.Rows[0]["value"].ToString() + "</label>";  
  224.                                 cell.Text += "</div>";  
  225.                                 OffCells.Add(cell);  
  226.                             }  
  227.   
  228.                         }  
  229.                         else  
  230.                         {  
  231.                             cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important; \">" + inner + "</label>";  
  232.                             cell.Text += "</br>" + r3["SensorID"].ToString();   
  233.                             cell.Text +=  "</br> Last Online: " + " N/A"+"</br>";  
  234.                             cell.Text += "</br> <label style=\"color:red\">" + "Last Value: N/A" + "</label>";  
  235.                             cell.Text += "</div>";  
  236.                             OffCells.Add(cell);  
  237.                         }  
  238.                         //cell.BorderStyle = BorderStyle.Solid;  
  239.                         //boardRow.Cells.Add(cell);  
  240.   
  241.                     }  
  242.                     count2++;  
  243.                 }  
  244.             }  
  245.         }  
  246.         TableRow OnRow = new TableRow();  
  247.           
  248.         TableRow OffRow = new TableRow();  
  249.         TableCell onHead = new TableCell();  
  250.         TableCell offHead = new TableCell();  
  251.         onHead.Text = "<div class=\"horoOn\">Online</div>";  
  252.         onHead.BackColor = System.Drawing.Color.Gray;  
  253.         onHead.ColumnSpan = 30;  
  254.         OnRow.Cells.Add(onHead);  
  255.   
  256.         offHead.Text = "<div class=\"horoOff\">Offline</div>";  
  257.           
  258.       
  259.         offHead.BackColor = System.Drawing.Color.Gray;  
  260.         offHead.ColumnSpan = 30;  
  261.         OffRow.Cells.Add(offHead);  
  262.         Unit p = new Unit(200, UnitType.Pixel);  
  263.         OnRow.Width = p;  
  264.         OnRow.Height = p;  
  265.         OffRow.Width = p;  
  266.         OffRow.Height = p;  
  267.         foreach (TableCell c in OnCells)  
  268.         {  
  269.               
  270.               
  271.             OnRow.Cells.Add(c);  
  272.               
  273.         }  
  274.         foreach (TableCell c2 in OffCells)  
  275.         {  
  276.               
  277.             OffRow.Cells.Add(c2);  
  278.               
  279.         }  
  280.   
  281.         //row.Cells.Add(cell);  
  282.         table1.Rows.Add(OnRow);  
  283.         table1.Rows.Add(OffRow);  
  284.       
  285. }  
 
Answers (2)