Adding a CSS class to an ASP.NET page from the code-behind: A step-by-step

I have been searching through numerous sites and feel frustrated:

Within my

<asp:table id="questionsTable" runat="server">
, I am dynamically adding rows and columns. My goal is to assign a predefined Cssclass to these newly created rows and columns, but I have not been successful.

The code for setting up the rows and columns:

TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TableCell cell3 = new TableCell();
TableCell cell4 = new TableCell();
TableCell cell5 = new TableCell();

I am aware that I can achieve this with:

row.Style.Add("width", "80%");
row.Style.Add("text-align", "left");

cell1.Style.Add("width", "10px");
cell2.Style.Add("width", "auto");
cell3.Style.Add("width", "75px");
cell4.Style.Add("width", "75px");
cell5.Style.Add("width", "75px");

This solution works, but it clutters the code-behind file.

I came across this alternative approach:

row.Attributes.Add("Class", "rowA");

// CSS - in StyleSheet.css
.rowA
{
    width: 80%;
    text-align: center;
    background-color: #FCF6CF;
}

However, it does not seem to be working for me...

Interestingly, when I inspect the generated markup source, I see this:

</tr><tr Class="rowA">

The above snippet is copied from the rendered page, yet the Css rules are not applied. I have confirmed that the Css is correct because manual application works as expected.

EDIT

I would like to express my gratitude to all who contributed to this issue. Unfortunately, an error occurred which led to the deletion of the link to the external stylesheet. Credit goes to Sven for pointing that out. After a long day like today, even I can make beginner mistakes.

Thank you once again.

Best regards,

Aiden

Answer №1

It is important that your class attribute is in lowercase (XHTML):

row.Attributes.Add("class", "rowA");

Additionally, ensure that you are including the CSS file on the page where you are displaying your table.

Answer №2

If the row's html output includes the specified class, it could mean that the corresponding css file is not correctly linked in the html document, or there may be a typo in the class name.

Answer №3

Your code behind is perfectly fine; the issue lies within your CSS.

Take a look at the W3 documentation on tables:

The horizontal layout of the table is determined by the table's width, column widths, and borders or cell spacing, not the contents of the cells.

If you want the rows to have an 80% width, make sure to set the table width to 80%. Here's how:

<asp:table id="questionsTable" runat="server" class="myTable">

.rowA
{
   text-align:center;
   background-color:#FCF6CF;
}

.myTable{
   width:80%;
}

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

Incorporate a background image property using Jquery

Can anyone help me with adding the css background-image:url("xxxxx") property to my code? jQuery('#'+$trackID+' .sc_thumb').attr('src',$thumb_url); jQuery('#'+$trackID+' .sc_container').css('display& ...

React-bootstrap's Modal is glitching and only partially appearing on the screen

I am a beginner in frontend development and I've been struggling to position the modal at the center of the screen; it keeps appearing on the right side. I am currently using "bootstrap/dist/css/bootstrap.min.css" for my CSS. Should I create a new CSS ...

Please send the element that is specifically activated by the onClick function

This is the HTML code I am working with: <li class="custom-bottom-list"> <a onClick="upvote(this)"><i class="fa fa-thumbs-o-up"></i><span>upvote</span></a> </li> Here is my JavaScript function for Upvot ...

What is the method for creating deployment files for an ASP.NET project without using the Visual Studio IDE?

After creating an ASP.NET MVC5 application in Visual Studio 2013, I am now faced with the issue of my evaluation version expiring. I require to make some modifications to my source code and regenerate deployment files. Is there a method to do this without ...

Retrieve variables from an external JavaScript file

I need to fetch the currentID variable from renderermain.js and utilize it in renderermem.js. However, I am looking for a way to achieve this without modifying mem.html: <script src="renderermain.js"></script> <script src="renderermem.js"& ...

div element's default input border displays in a distinct manner

When I create an input box without any styles (<input>) and then inspect the computed CSS in devtools, it shows a border of 2px inset rgb(238, 238, 238). However, if I create a div with the same border properties, it looks noticeably different: ...

Issue encountered while exchanging data between POST request and GET method within a Django framework

Currently, I am working on a select tag that triggers a search in the database for similar records once a user has made a selection. The challenge lies in accessing this data effectively. Upon selecting an option, the user is directed to another page tha ...

Utilizing either AJAX or PHP to input information into a predetermined section within a text document

I am currently in the process of developing a website that involves intricate table structures and text files acting as databases. At the moment, I have a page that displays all the available data to the user, shown below: all_data <html> <h ...

Bx slider - displaying partially hidden slides at the far right while utilizing a full-width carousel

Implementing a slider on my nodejs website using bx slider has been quite an experience. Below is how I achieved it: $(document).ready(function(){ $(".bxslider").bxSlider({ minSlides: 1, maxSlides: 40, slideWidth: 300, ...

What is the best way to activate a scrollbar exclusively for the final section in fullpage.js?

Is there a way to enable a scrollbar for the last section only in fullpage.js? This is what I attempted: $("#section3").fullpage({ scrollBar: true, fitToSection: false, }); Unfortunately, it didn't work as expected. ...

Present fetched data from ajax in a tabular format

I'm working on an AJAX call that retrieves a JSON array from a PHP file. My goal is to populate the rows within the #gamesList table with this data. I already have the append function set up, but I'm struggling with iterating through the returne ...

Looking for the decimal to accurately store and display up to three decimal places

I am encountering an issue with a line of code in c# where the textbox always resets back to the format 0.00. How can I modify it to maintain the format 0.000 instead? NewYorkTax = Convert.ToDecimal(txtNewYorkTax.Text); //The conversion results in 0.00 f ...

Ways to extract the value of radio buttons for easy manipulation purposes

Update: Success! I finally got it to work. Here is my example code. Hopefully, this can assist others with similar needs. I am in need of a shipping radio button for regular and express shipping options. With some guidance from online research and cale_b ...

Steps for implementing remote modals in Bootstrap 5 using CS HTML

I'm attempting to implement a remote modal window in Bootstrap 5 with C# MVC. The endpoint for the modal partial view is configured correctly. According to this discussion on Github, all that needs to be done is to call some JavaScript. However, it ...

solve the issue of images getting resized in bootstrap 4

I'm facing a challenge with maintaining the size of logo images within their "circle containers" regardless of the browser width. The containers are set at 100px by 100px using the following CSS: .timeline-image::before { content: ""; width: ...

Django: showcasing a related model's key in a template

I am new to Django and struggling with displaying a foreign key in the template. In my Job model, I have 'employer' as a ManyToManyField: from django.db import models # Create your models here. class Employer(models.Model): id = models.Auto ...

Convert your flexible designs into dynamic HTML with our Flex to HTML/A

Looking for a solution to transform Flex code into Html/Ajax code. Most of my Flex code includes datagrids, buttons, and text labels. ...

Focusing on a particular website using the Azure Monitor Application Insights Agent for precise monitoring and analysis

I am currently in the process of setting up the Azure Monitor Application Insights Agent on my personal computer by following the instructions outlined on this Microsoft Azure documentation page. I have successfully created an Application Insights resource ...

Grid items in Material UI are not positioned next to each other

I am encountering an issue with the Grid component in material-ui. The grid items are currently stacking below each other instead of beside each other, and I'm unsure of what is causing this behavior. My intention is for the grid items to stack on top ...

Troubleshooting the Height Problem in Bootstrap Divisions

Can someone help me align the height of the alert box with the input field? I've tried using Bootstrap classes but can't seem to get it right. I attempted using col-mb-4 in the alert class, but the height alignment is still off. <link rel= ...