Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatically apply style to an asp.net website

with a new Empty website application

i would like to apply a style to the aspx Page via a fie that holds some custom values of css attributes ,i am not sure which is better approach.

i am still testing the concept , i have a file that holds those values :

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

these values are not hardcoded but generated by another application

and i would like to read it into the application .

i could think of the only two ways i know :

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

then through code behind set the html elements style properties via proccessed data

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

OR

I could also load style sheet 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 put a C# server code like below -->

<%=someVariableOrCsMethodReturnedValue%>
</head>

so that will hold a formatted style with the loaded values.

is that the way to load custom values for css style ?

like image 677
LoneXcoder Avatar asked Dec 15 '22 16:12

LoneXcoder


2 Answers

You can load CSS to an object in .NET by using

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

However, this is not recommended for usage because you make it inline coding for css and upper css settings will not be applied if you have the same attribute.

The best approach will be setting an external CSS class and set all of them in there:

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

And in your CSS class having:

.exampleClass{width:100px; width:130px; background-color:#aac93f}
like image 116
Berkay Turancı Avatar answered Dec 28 '22 11:12

Berkay Turancı


The <style> tag can also be used as server control:

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

This will generate a field of type HtmlGenericControl in the page.

In one of the page life-cycle events (Page_Load, Page_Init, etc), assign the literal CSS definition like this:

var css = @"
body
{
  background-color:#b0c4de;
}";
htmlCss.InnerHtml = css;
like image 26
devio Avatar answered Dec 28 '22 10:12

devio