Automate the application of design to an asp.net web platform using code

Starting fresh with a brand new Empty website application

I'm looking to apply a unique style to the aspx Page using a file that contains custom CSS attribute values. I'm not sure which approach would be better.

I'm still in the testing phase, and I have a file that contains these values:

width;100px    width;130px    background-color;#aac93f

These values are dynamically generated by another application.

I want to read this data into the application. The only two methods that come to mind are:

`File.ReadAllLines` or `File.ReadAllText`.

Then, through the code-behind, I would set the HTML elements' style properties based on processed data:

htmltag.Style.Add("width", setting1)....etc

OR

Another option could be to load the stylesheet from dynamic/programmatic data:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

<!--   and include C# server code like below -->

<%=someVariableOrCsMethodReturnedValue%>
</head>

This way, a formatted style can be applied with the loaded values.

Is this the recommended method for loading custom CSS values?

Answer №1

To apply CSS to an object in .NET, you can use the following method:

objectName.Attributes.Add("style", "width:100px; width:130px; background-color:#aac93f");

However, it is not recommended to do inline coding for css as previous css settings may not be applied if you have the same attribute.

A better approach would be to define an external CSS class and assign all styles there:

objectName.Attributes.Add("class", "exampleClass");

Then, in your CSS file, include the following:

.exampleClass{width:100px; width:130px; background-color:#aac93f}

Answer №2

Utilizing the <style> tag as a server control is another option as well, which can be found here:

<style type="text/css" runat="server" id="htmlCss"></style>

This action will result in the creation of a field with the type HtmlGenericControl on the page.

During one of the events in the page life-cycle (Page_Load, Page_Init, etc), simply assign the CSS definition using this method:

var css = @"
body
{
  background-color:#b0c4de;
}";
htmlCss.InnerHtml = css;

Answer №3

Another approach is to embed your custom CSS within an asp.net Literal control placed within your webform, which will then render all the necessary styles like this...

In your backend code:

Literal1.Text = "<style>" + File.ReadAllText(filepath) + "</style>";

As a result, your webpage will display:

<style>
    .class1
    {
        width: 100px;
    }
  
    .class2
    {
        width: 100px;
    }
</style>

Subsequently, apply these classes to your respective elements throughout your page.

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

Parsing a JSON data with JObject and LINQ techniques

Encountered a situation where I need to deserialize a large JSON object with specific conditions. { "fields": [ { "webhook": true, "associated_lookup": null, "json_type": "jsonobject& ...

Challenge in creating a responsive layout using Bootstrap framework

I'm working with Bootstrap, but I'm struggling to achieve the desired layout. My goal is to have the design adapt like this on different screen sizes: ---------------------- Headline | | | image | ----------| | B ...

ASP.NET MVC Ajax will solely refresh the first item in the list

On my index page, I have a collection of Posts where users can vote up or down on each one. However, I am encountering an issue where I am only able to vote on the first post in the list. This is what my JavaScript code looks like: <script type="text/ ...

What is the best way to prevent my images from resizing in a Bootstrap 4 carousel?

I am working on a website where users can upload images of various sizes. These images are then displayed in a Bootstrap 4 carousel. I want to ensure that the carousel maintains a consistent dimension across different screen sizes without changing the widt ...

Utilizing connection pooling for efficient resource management and seamless single sign on

In order to achieve single sign-on within an intranet environment, an ASP.NET 3.5 application must utilize SQL Server 2005 and utilize Windows Authentication mode in the web.config. One of the objectives is to maximize the use of connection pooling. Addit ...

Endless time continuum within the scheduling application

Looking for a way to make the time axis in my scheduling app infinite? Currently, it has a fixed length, but I want users to be able to scroll endlessly into the past or future. Any suggestions on how to achieve this? Check out this JSBin for a basic exam ...

What is the most reliable method for displaying an SVG shape in any color across various web browsers?

Is there a way to render a 2D SVG shape, which is flat and 100% black, in any color across various browsers? ...

What is the procedure for retrieving the values stored in a RadGrid?

In my RadGrid, I have data bound to an IDictionary of string keys and values. There are two GridTemplateColumns displaying textboxes, one for the Key and the other for the Value. Users can edit these values. How can I retrieve an IDictionary containing al ...

Tips for utilizing javascript to reset the heightline of the preceding keyword during a keyword search

Using JavaScript, I have implemented a search keyword function that highlights specific words in a paragraph. However, I am facing an issue. Currently, when searching for the next keyword, the previously highlighted text remains in red instead of reverting ...

Eliminate the gaps between the columns of the table

Is there a way to eliminate the gap between the day, month, and year columns in this table? <form action='goServlet' method='POST'> <table style="width:1000px" style="text-align:center"> <tr style="text-ali ...

Issue with scrollHeight in Vue.js component

I am currently using Vue and have a div tag styled with CSS var max_pages = 100; var page_count = 0; function snipMe(el) { page_count++; if (page_count > max_pages) { return; } var h = parseInt(window.getComputedStyle(this, null).getP ...

Div text with superfluous line breaks

Whenever a user clicks the sign-up button ("Cadastrar" in Portuguese) on my website, the newsletter form successfully hides. However, there seems to be unnecessary line breaks in the success message. Why is this happening? I am trying to make sure that th ...

Authenticate using tokens in ASP.NET MVC 6

There appears to be a lack of information available regarding authorization in the latest MVC version. With ASP 5 now in RC 1, it's time to delve into understanding how this process will function... My goal is to create a basic example of an authenti ...

Creating Dynamic Height for Div Based on Another Element's Height Using ReactJS and CSS

I'm attempting to set a fixed height for a div in order to enable overflow scrolling. However, I am encountering issues as I am using JavaScript within a useEffect hook to accomplish this task. The problem is inconsistent as sometimes the height is se ...

What is the proper method for calling a native function within a class?

I'm working with a dll that has a function named MyClass::MyMethod(char*). Can someone guide me on how to utilize this using DllImport? ...

Decrypting an AES string encrypted in C# using Java

I have an encrypted string in Java using "AES", and now I need to decrypt it in my C# program (I have the key). How can I decrypt it in C#? What is the process for decoding it in C#? Here is my encryption and decryption logic in Java: public class AES { ...

What is the necessity of having a ref in this particular situation?

While examining the reference source, I came across NumberToInt32: [System.Security.SecuritySafeCritical] // auto-generated internal unsafe static Int32 ParseInt32(String s, NumberStyles style, NumberFormatInfo info) { Byte * numberBufferBytes = s ...

Mandrill: Sorry, the server seems to be experiencing some technical difficulties with a 500 internal server error

I have utilized the following code snippet to send emails using Mandrill: public bool sendMessage(string from, List<To> to, string subject, string body,out string rsText) { var host = "https://mandrillapp.com/api/1.0"; if (k ...

Transforming database information into a JSON format

When attempting to convert database data into JSON, the output I receive using JavaScriptSerializer from this site looks like: [{"Yes":6,"No":1,"Maybe":4}]. My database table has columns named Yes, No, and Maybe. However, I need the JSON format to be str ...

Guide to accessing an ASP.NET application from a different computer within the shared network

Currently, I am running an ASP.NET application on my PC and I am interested in accessing this application from another PC within the same network. In my attempts to access the application, I input the URI of my PC on the network, followed by the port numbe ...