none
Dynamic HTML from Code Behind RRS feed

  • Question

  • 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

            protected void Page_Load(object sender, EventArgs e)
            {
                if (this.Session["UserID"] == null)
                {
                    Response.Redirect("Login.aspx");
                }
                    Response.AppendHeader("Refresh", "8");
                    HtmlMeta meta = new HtmlMeta();
                    meta.HttpEquiv = "Content-Language";
                    meta.Content = "en";
                    MetaPlaceHolder.Controls.Add(meta);
                    List<TableCell> OnCells = new List<TableCell>();
                    List<TableCell> OffCells = new List<TableCell>();
    
                    DataTable dt = DatabaseObjects.UserDO.GetAllBoardID(Int32.Parse(this.Session["UserID"].ToString()));
    
                    foreach (DataRow r in dt.Rows)
                    {
                        string inner = r["DeviceType"].ToString();
    
                        
                        TableCell cell = new TableCell();
    
                        DataTable FonaDT = new DataTable();
                        DataTable dt2 = new DataTable();
                        DataTable fonaReturn = new DataTable();
                        DataTable normalReturn = new DataTable();
                        DateTime now = new DateTime();
    
                        if (DatabaseObjects.UserDO.IsFona(r["BoardID"].ToString()))
                        {
                            FonaDT = DatabaseObjects.UserDO.GetFonaSensors(r["BoardID"].ToString());
                        }
                        else
                        {
                            dt2 = DatabaseObjects.UserDO.GetSensorIDFromBoardID(r["BoardID"].ToString());
                        }
                        //cell.Text = "<label style=\"color:red; font-size:large;\">Device :</label><label style=\"color:#67BCDB; font-size:large;text-decoration:underline;\">" + inner + "</label>";
                        //cell.BackColor = System.Drawing.Color.Gray;
                        //cell.ColumnSpan = 30;
                        if (FonaDT.Rows.Count > 0)
                        {
                            //sensorRow.Cells.Add(cell);
                            int count = 0;
                            foreach (DataRow r2 in FonaDT.Rows)
                            {
                                if (count > 0)
                                {
                                    cell = new TableCell();
                                    //cell.Text += r2["sensor_name"].ToString();
                                    cell.HorizontalAlign = HorizontalAlign.Center;
                                    fonaReturn = DatabaseObjects.UserDO.GetDashboardData(r["BoardID"].ToString(), r2["sensor_name"].ToString());
                                    if (fonaReturn.Rows.Count > 0)
                                    {
                                        now = DateTime.Now;
                                        DateTime LastData = DateTime.Parse(fonaReturn.Rows[0]["ts_Datetime"].ToString());
                                        TimeSpan diff = now.Subtract(LastData);
                                        if (diff.TotalSeconds <= 120)
                                        {
                                            //Its on
                                            cell.Text += "<div class=\"badgeOn\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";
                                            cell.Text += "</br>" + r2["sensor_name"].ToString();  
                                            cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + fonaReturn.Rows[0]["value"].ToString() + "</label>";
                                            cell.Text += "</div>";
                                            OnCells.Add(cell);
                                        }
                                        else
                                        {
                                            //its off
                                            cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";
                                            cell.Text += "</br>" + r2["sensor_name"].ToString();  
                                            cell.Text += "</br> Last Online: " + LastData.ToString("MM/dd/yyyy hh:mm tt");
                                            cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + fonaReturn.Rows[0]["value"].ToString() + "</label>";
                                            cell.Text += "</div>";
                                            OffCells.Add(cell);
                                        }
    
                                    }
                                    else
                                    {
                                        cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";
                                        cell.Text += "</br>" + r2["sensor_name"].ToString();  
                                        cell.Text +=  "</br> Last Online: " + " N/A"+"</br>";
                                        cell.Text += "</br> <label style=\"color:red\">" + "Last Value: N/A" + "</label>";
                                        OffCells.Add(cell);
    
                                    }
                                    //cell.BorderStyle = BorderStyle.Solid;
                                    //boardRow.Cells.Add(cell);
    
                                }
                                else
                                {
                                    cell = new TableCell();
                                    //cell.Text += r2["sensor_name"].ToString() + "</br>";
                                    cell.HorizontalAlign = HorizontalAlign.Center;
                                    fonaReturn = DatabaseObjects.UserDO.GetDashboardData(r["BoardID"].ToString(), r2["sensor_name"].ToString());
                                    if (fonaReturn.Rows.Count > 0)
                                    {
                                        now = DateTime.Now;
                                        DateTime LastData = DateTime.Parse(normalReturn.Rows[0]["ts_Datetime"].ToString());
                                        TimeSpan diff = now.Subtract(LastData);
                                        if (diff.TotalSeconds <= 120)
                                        {
                                            //Its on
                                            cell.Text += "<div class=\"badgeOn\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";
                                            cell.Text += "</br>" + r2["sensor_name"].ToString();  
                                           
                                            cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + fonaReturn.Rows[0]["value"].ToString() + "</label>";
                                            cell.Text += "</div>";
                                            OnCells.Add(cell);
                                        }
                                        else
                                        {
                                            //its off
                                            cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";
                                            cell.Text += "</br>" + r2["sensor_name"].ToString();  
                                            cell.Text += "</br> Last Online: " + LastData.ToString("MM/dd/yyyy hh:mm tt");
                                            cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + fonaReturn.Rows[0]["value"].ToString() + "</label>";
                                            cell.Text += "</div>";
                                            OffCells.Add(cell);
                                        }
    
                                    }
                                    else
                                    {
                                        cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";
                                        cell.Text += "</br>" + r2["sensor_name"].ToString();
                                        cell.Text += "</br> Last Online: " + " N/A" + "</br>";
                                        cell.Text += "</br> <label style=\"color:red\">" + "Last Value: N/A" + "</label>";
                                        cell.Text += "</div>";
                                        OffCells.Add(cell);
                                    }
                                    //cell.BorderStyle = BorderStyle.Solid;
                                    //boardRow.Cells.Add(cell);
    
                                }
                                count++;
                            }
                        }
                        if (dt2.Rows.Count > 0)
                        {
                            //sensorRow.Cells.Add(cell);
                            int count2 = 0;
                            foreach (DataRow r3 in dt2.Rows)
                            {
    
                                if (count2 > 0)
                                {
                                    cell = new TableCell();
                                    //cell.Text += r3["SensorID"].ToString() + "</br>";
                                    cell.HorizontalAlign = HorizontalAlign.Center;
                                    normalReturn = DatabaseObjects.UserDO.GetDashboardData(r["BoardID"].ToString(), r3["SensorID"].ToString());
                                    if (normalReturn.Rows.Count > 0)
                                    {
                                        now = DateTime.Now;
                                        DateTime LastData = DateTime.Parse(normalReturn.Rows[0]["ts_Datetime"].ToString());
                                        TimeSpan diff = now.Subtract(LastData);
                                        if (diff.TotalSeconds <= 120)
                                        {
                                            //Its on
                                            cell.Text += "<div class=\"badgeOn\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";
                                            cell.Text += "</br>" + r3["SensorID"].ToString();  
                                            
                                            cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + normalReturn.Rows[0]["value"].ToString() + "</label>";
                                            cell.Text += "</div>";
                                            OnCells.Add(cell);
                                        }
                                        else
                                        {
                                            //its off - first most common
                                            cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";
                                            cell.Text += "</br>" + r3["SensorID"].ToString();  
                                            cell.Text +=  "</br> Last Online: " + LastData.ToString("MM/dd/yyyy hh:mm tt");
                                            cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + normalReturn.Rows[0]["value"].ToString() + "</label>";
                                            cell.Text += "</div>";
                                            OffCells.Add(cell);
                                        }
    
    
                                    }
                                    else
                                    {
                                        cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";
                                        cell.Text += "</br>" + r3["SensorID"].ToString();
                                        cell.Text += "</br> Last Online: " + " N/A" + "</br>";
                                        cell.Text += "</br> <label style=\"color:red\">" + "Last Value: N/A" + "</label>";
                                        cell.Text += "</div>";
                                        OffCells.Add(cell);
                                    }
                                    //cell.BorderStyle = BorderStyle.Solid;
                                    //boardRow.Cells.Add(cell);
    
                                }
                                else
                                {
                                    cell = new TableCell();
                                    normalReturn = DatabaseObjects.UserDO.GetDashboardData(r["BoardID"].ToString(), r3["SensorID"].ToString());
                                    //cell.Text += r3["SensorID"].ToString() + "</br>";
                                    cell.HorizontalAlign = HorizontalAlign.Center;
                                    if (normalReturn.Rows.Count > 0)
                                    {
                                        now = DateTime.Now;
                                        DateTime LastData = DateTime.Parse(normalReturn.Rows[0]["ts_Datetime"].ToString());
    
                                        TimeSpan diff = now.Subtract(LastData);
                                        if (diff.TotalSeconds <= 120)
                                        {
                                            //Its on
                                            cell.Text += "<div class=\"badgeOn\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";
                                            cell.Text +="</br>"+ r3["SensorID"].ToString(); 
                                            
                                            cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + normalReturn.Rows[0]["value"].ToString() + "</label>";
                                            cell.Text += "</div>";
                                            OnCells.Add(cell);
                                        }
                                        else
                                        {
                                            //its off - second most common
                                            cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important;\">" + inner + "</label>";
                                            cell.Text += "</br>" + r3["SensorID"].ToString(); 
                                            cell.Text +=  "</br> Last Online: " + LastData.ToString("MM/dd/yyyy hh:mm tt");
                                            cell.Text += "</br> <label style=\"color:red\">" + "Last Value: " + normalReturn.Rows[0]["value"].ToString() + "</label>";
                                            cell.Text += "</div>";
                                            OffCells.Add(cell);
                                        }
    
                                    }
                                    else
                                    {
                                        cell.Text += "<div class=\"badgeOff\">" + "<label style=\"font-size:small !important; color:#67BCDB;text-decoration:underline; font-weight:bold !important; \">" + inner + "</label>";
                                        cell.Text += "</br>" + r3["SensorID"].ToString(); 
                                        cell.Text +=  "</br> Last Online: " + " N/A"+"</br>";
                                        cell.Text += "</br> <label style=\"color:red\">" + "Last Value: N/A" + "</label>";
                                        cell.Text += "</div>";
                                        OffCells.Add(cell);
                                    }
                                    //cell.BorderStyle = BorderStyle.Solid;
                                    //boardRow.Cells.Add(cell);
    
                                }
                                count2++;
                            }
                        }
                    }
                    TableRow OnRow = new TableRow();
                    
                    TableRow OffRow = new TableRow();
                    TableCell onHead = new TableCell();
                    TableCell offHead = new TableCell();
                    onHead.Text = "<div class=\"horoOn\">Online</div>";
                    onHead.BackColor = System.Drawing.Color.Gray;
                    onHead.ColumnSpan = 30;
                    OnRow.Cells.Add(onHead);
    
                    offHead.Text = "<div class=\"horoOff\">Offline</div>";
                    
                
                    offHead.BackColor = System.Drawing.Color.Gray;
                    offHead.ColumnSpan = 30;
                    OffRow.Cells.Add(offHead);
                    Unit p = new Unit(200, UnitType.Pixel);
                    OnRow.Width = p;
                    OnRow.Height = p;
                    OffRow.Width = p;
                    OffRow.Height = p;
                    foreach (TableCell c in OnCells)
                    {
                        
                        
                        OnRow.Cells.Add(c);
                        
                    }
                    foreach (TableCell c2 in OffCells)
                    {
                        
                        OffRow.Cells.Add(c2);
                        
                    }
    
                    //row.Cells.Add(cell);
                    table1.Rows.Add(OnRow);
                    table1.Rows.Add(OffRow);
                
            }
    

    • Moved by CoolDadTx Thursday, December 7, 2017 6:04 PM ASP.NET related
    Thursday, December 7, 2017 4:11 PM

All replies