简体   繁体   中英

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 ?

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}

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;

You could also write your custom CSS into an asp.net Literal object that you place somwhere in your webform which would render all your css like this...

In your code behind:

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

In your page would render:

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

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

Then have each of your elements reference the classes included in your css.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM