I'm working on a website using ASP.NET C# code, the website will be used for a variety of websites and only serve as a 'placeholder'. We would love to have CSS code which can have different markup depending on which website you are visiting. We're pulling information about the website someone is visiting from our database, which has some methods attached to return these values.
The problem is that we want to be able to use these values in CSS code, and currently the only way I can think of doing this is by using inline CSS code instead of .css files. Which would make our code look something like:
<style>
.navbar {
background-color: #Website.Models.WebsiteConfiguration.NavbarColor;
}
</style>
Which isn't ideal. Is there another way of using C# variables in CSS code, without using inline code? I've found a website which describes using a custom handler to modify the css files, however we couldn't get this to work because our parser was never called. We also found the .LESS library, but we would rather not use this library, and instead work on a solution that only uses a couple lines of code.
You could use some sort of templating language to render the css files. On it's own variables in css won't be populated but you could write a utility class to read the template file, populate variables, save new file to appropriate location and output the correct style element on the page, pointing to new css file.
That way you could have something like the following that would render the appropriate css:
<% CssHelper.Load("myFile.template", "websitex/style.css")%>
Would that be a viable option?
Related
I have some pre-compiled html content which I want to include in my View like:
#*View Start*#
Some precompiled html
Some view content
Some precompiled html
Some view content
Some precompiled html
#*View End*#
I have already thought of some ways but each of them has some GREAT downsides that I don't want to use it. these are the ways I have thought of:
Although these html codes are pre-compiled with fixed content they may change time to time (let say weekly) so can not be included in view itself
They're rather big in size so I don't think having them in database would be wise (would increase database size and data-bandwith)
Having them in html files and writing them into view using C# functions something like #File.ReadAllText("page-customized-head.html") and this would be slow and would make hard disk busier than it should.
I want to know if anybody can suggest a better solution or a way to improve the above solutions.
Edit:
I had put aside solutions like ajax, as in this situation was not suitable for my design. After #Hadee's comment I noticed that my description is not complete, so I'm adding some more description.
These content files can be unique for different pages of different users as user can customize css, js. add - edit remove html elements. So each user may have several pages that each page may have several different "Pre-Compiled" section.
As these content may be the head section of the page, may contain css, js (that following content may rely on it), ... ajax is not suitable in here.
And as for partial views, I don't see any different between them and having the content written into html file. Actually I think html file and #File.ReadAllText("page-customized-head.html") would be faster, as unlike .cshtml it does not need compiling.
I was just wondering is it possible to edit a websites source clientside using C#. Like say I wanted to change webkit-user-select from none to inherited.
body {
background: #000;
color: #333;
margin: 0;
padding: 0;
-webkit-user-select: none;
}
I was wanting to add the option to the pageloadcomplete section, Just don't know where to begin. Never done that.
It sounds like you want to temporarily modify the CSS file of (I assume the same project as your C# web site?) so it renders differently, but you want to do it from C# some how?
If so, the answer is you can, but I don't recommend it. But you can't do it in the ASPX WebForms page request, because the link in the CSS file that the ASPX request links to is a separate HTTP request completely.
Instead, you would have to write an HTTP handler and run all your CSS requests through it, manually load the CSS file, parse it, and somehow determining how and when to modify the response. Way too much work; I don't recommend it.
I think the simpler solution would be to have an extra CSS class in the file that doesn't normally apply to the element you want it to, then render client-side JavaScript in your ASPX page so that when the page loads (in the client) the JavaScript can add the CSS class to the element you need it to so different CSS applies.
There are lots of ways to write JavaScript to add/remove classes, both with libraries or in direct code. Google search for how to do that, there are a lot of basic tutorials on altering CSS classes on DOM elements via JavaScript.
IF the assumptions above are wrong, and you want to modify the CSS of another site you don't control, the answer is again, technically yes, but an even more emphatic "it's not worth it." You'd have to write a whole C# application to load the web site and all its resources, intercept the CSS file before being loaded into whatever renderer you're using, write code to parse the CSS and adjust it, etc. There's no reason to go to all that effort.
It would be a little more reasonable to write a browser plugin to do it, but again, that's a ton of work too.
The following code access the sharepoint list.
var ctx = new ClientContext("http://tests.com");
var lists = ctx.Site.RootWeb.Lists;
ctx.Load(lists);
ctx.ExecuteQuery();
foreach (var list in lists)
Console.WriteLine(list.News);
Console.ReadKey();
In lists.News, lets assume one of the fieldvalues has html body and only the html content is pulled and shown in output:
<html>
<div class="ExternalClass2D015247C9464EC29A11153E0E0BAEA5">
<h1>Hey this is flash news</ h1> etc ...< /div>
</html>
Here, how to access the content of this CSS class using C#, so that there is no need to regenerate similar CSS? or otherwise, can we get reference to the CSS files, that way the css link can be referred in html ?
The goal is to fetch the HTML file, which was generated in Sharepoint using inbuilt RTF document and use it as source to show in different web application. Needed the html content and its related resources(CSS) either reference or actual copy of the content.
Thanks for taking time to read my question and answer. Yes, it's the source of rich text field. I agree the rich text field is using the default site's css files and its not accessible. Besides based on my research so far, I believe to solve this issue and enable the flexibility to use custom css, we may reset the SharePoint default css settings to use alternate user provided CSS file and let the RTF generate content utilizing this new CSS. This way we can share this css file both in share point and when the html source is outsourced to another application.
Note: To change settings we would need the 'Site Collection Admin' and 'Site Admin' Access/privilege enabled. The CSS settings are changed under 'Master Page' settings.
New file is added by clicking on 'Site Content' on main page. Windows explorer option can be used to create/import new css file.
When writing CSS there are some style values that require a prefix (-webkit-, -moz-...). Is there any C# parser out there that can add this vendor specific prefixes? (Free, open source>).
I would like to add a HttpHandler that would return the browser specific CSS, while writing without adding prefixes.
I should note this post - it dose not provide a suitable c# answer.
(Similar to http://cssprefixer.appspot.com/).
I am working in VS.
I've a question related to dynamic code generation (html) with C# based on a template. The user sets all options (e.g. whether or not a <div> should be displayed, whether or not an image in the template should change by a specific date,...) and provides the content (text, images) needed for code generation. The generated code is needed for a WIFI hotspot site.
The template consists of HTML, CSS and JavaScript which gets extended and modified by the user-defined settings at runtime. I also think of providing "meta settings" in the template to define, whether or not some options CAN be disabled set or not.
My major problem:
How to define the template to dynamically extend the template code easily? For example, if a user option is enabled at runtime, I'm in need to add JavaScript code on top and HTML code below another (specific) place. Another example is to hide content (a defined ) when another option is disabled...
I'm not sure what's the best practice to handle that requirement. Maybe HTML elements with id attribute can help eliminate some of the problems. But with JavaScript I'm not sure. Maybe the template needs to be a XML which creates the final HTML at runtime? Any idea?
Has anyone an idea how to handle that?
UPDATE/INFO:
The project is written in pure C# - without any ASP.NET. It is a desktop project working with HTML files and GENERATING HTML files (as an output).
I suppose you can use ASP.NET MVC Razor view engine to render an html to a file with the help of this question. With Razor you will get the support of dynamic view (aka template) changes, rich template syntax etc. Everything you can do when create a web site. Just render the html to a file not a response body.