Input various colored text within an HTML element attribute

In my asp.net project, I am looking to dynamically change the text color of a table cell based on a certain parameter. Here's an example scenario:

TableCell dataCell = new TableCell();
            foreach (var o in results)
            {
                TimeSpan timeDiff = (DateTime.Now - o.time);
                if (timeDiff.TotalSeconds < 60.0)
                {
                    //Display in green color
                    dataCell.Text += o.name;
                    //I attempted using string.Format("<p style=\"color:green\">{0}</p>", o.name); but it didn't work.
                }
                else
                {
                    //Display in red color
                    dataCell.Text += o.name;
                }                   
            }
            TRow.Cells.Add(dataCell);  

I specifically require the text to be displayed in a single line which is why <h3>, <div>, and <p> elements do not suit my needs.

Answer №1

If you want to change the text color of the entire cell, use the ForeColor property:

if (timeDiff.TotalSeconds < 60.0)
{
    //Text with green color
    dataCell.ForeColor = System.Drawing.Color.Green;
    dataCell.Text += o.name;
}
else
{
    //Text with red color
    dataCell.ForeColor = System.Drawing.Color.Red;
    dataCell.Text += o.name;
}

Alternatively, you can add other controls like Labels to the cell. Keep in mind that if you do this dynamically, you'll need to recreate these controls on every postback.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble arises when trying to utilize JsonUtility.FromJson in Unity

Encountering a problem while attempting to parse a JSON in Unity. The error message "ArgumentException: JSON parse error: Invalid value." keeps popping up. The structure of my JSON data is as follows: [{"equipo":"Boca","pj":"5","pg":"3","pp":"2"}] Below ...

AngularJS directive element not displaying CSS styles properly

I've been grappling with this issue for the past few days and I just can't seem to crack it. Whenever I try adding a class to an AngularJS element directive, the styling appears in the code view of my browser (Chrome), but the styles themselves ...

Save information in a session using html and javascript

I'm having trouble accessing a session variable in my javascript code. I attempted to retrieve it directly but ran into issues. As an alternative, I tried storing the value in a hidden HTML input element, but I am unsure of how to properly do that wit ...

Update the color of navigation items to reflect their active status

Here is the snippet of HTML code: <header> <nav> <a href="#" id="menu-icon"></a> <ul> <li><a href="#"">Home</a></li> <li><a href="#">About</a></li> & ...

Issue with Bootstrap Navbar dropdown causing page refresh instead of menu dropdown activation

<!DOCTYPE html> <html lang="en" dir="ltr" style="style.css"> <!-- All icons were courtesy of FlatIcon : www.flaticon.com and FreePik : www.freepik.com --> <head> <meta charset="utf-8"&g ...

How can we detect line breaks within a selection using JavaScript?

Apologies for the lack of expertise in the content below, as it is being produced by a designer experimenting with coding :D The goal here is to determine the number of lines selected or highlighted by the cursor. When I mention "lines," I mean what is vi ...

Animate an image to the right when clicked, then return it to the left with a second click

Seeking help with animating a set of images to move individually 300px right on first click, and then 300px left when clicked again. I'm currently facing an issue where my code is not working. It could be due to A) syntax errors or B) the images not ...

Button that triggers HTML Audio play when clicked

Is it possible to have audio play when Button 1 is clicked, but then pause or stop if Buttons 2 or 3 are clicked instead? <a href="#" class="button1">Button 1</a> <a href="#" class="button2">Button 2</a> <a href="# ...

When utilizing React, I generated an HTML page with a distinct .js file, however, encountered two unexpected errors

Update : Gratitude to everyone who has helped me in tackling this issue. One user suggested using a web server, but my intention was to find a solution using just a single HTML and JS file. Even though I tried following the steps from a similar question o ...

Using jQuery to populate tables

Here is a table I am working with: <table class="table table-hover"> <thead> <tr> <th>Person</th> <th>Ethereum Address</th> ...

Firefox compatibility issue: Bootstrap modal button not functioning properly when wrapping other HTML elements

Hey everyone, I've come up with some awesome image hover effects that you can use. I've implemented bootstrap modals so that when you click on 'show code', a pop-up will display with the HTML and CSS codes for easy copy-pasting. However ...

The CSS menu appears more compact when viewed on a different page

Recently, I've been working on a website for practice and decided to include a menu. It functions perfectly on the first webpage, but appears slightly smaller on the second one. Here's the code snippet I used for the menu on the initial page: &l ...

Sequential Element Loading using jQuery upon pageload

Can someone please explain how to achieve the effect shown on this website: http://www.getflow.com/ ? I believe that jQuery is being used to gradually adjust the opacity of each element. Is there a simple JavaScript snippet available to load each image e ...

Ways to display an icon in Angular 10 only when it is hovered over

Just getting started with Angular and still learning the concepts. Trying to figure out how to show an icon only when it's hovered over. Can anyone assist me? Sorry, can't share the code because of company rules. The icons are for sorting and ...

How can you apply styling to one element when focusing on a different element at separate hierarchy levels?

I'm currently facing a challenge with styling a text field to expand when focused, and adding an "Add" button below it. The issue is that the elements are on different levels in the code structure, making it difficult to show or hide the button when f ...

Spacing before input text is found to be unnecessary and can be removed

Hello there, I've encountered a slight issue with a text input field that has border radius. The first character typed seems to protrude slightly outside the input field due to the border radius. Is there a way to add some extra whitespace before the ...

Tips for receiving form data in your asp.net mvc controller

I have tried to send data to the controller using the $.ajax method in my view, but I am unable to retrieve the posted data. I have attempted multiple approaches to retrieve the data without success. Below is the code that I have posted: $.ajax({ ...

Charge me at a later date directly from the shopping cart

We need to integrate the BILL ME LATER feature into our shopping cart. The challenge is to have a scenario where customers can add products to their cart and then choose from three options for checkout: 1. Regular Checkout 2. PayPal Checkout 3. BILL ME LAT ...

implementing a smooth transition effect for image changes upon hover

I've been working on a React project where I created a card that changes its image when hovered over. I wanted to add a smoother transition effect to the image change using transition: opacity 0.25s ease-in-out;, but it doesn't seem to be working ...

Altering the display attribute cancels out any opacity transitions

When using JavaScript to change the opacity of an element with transition: opacity 1s, the expected duration of one second is met, as demonstrated here: onload = e => { var p = document.getElementsByTagName("p")[0]; p.style.opacity = 1; }; p { ...