How to make user controls know about css classes in ASP.NET - asp.net

Since there are no header sections for user controls in asp.net, user controls have no way of knowing about stylesheet files. So css classes in the user controls are not recognized by visual studio and produces warnings. How can I make a user control know that it will relate to a css class, so if it is warning me about a non-existing css class, it means that the class really do not exist?
Edit: Or should I go for a different design like exposing css classes as properties like "HeaderStyle-CssClass" of GridView?

Here's what I did:
<link rel="Stylesheet" type="text/css" href="Stylesheet.css" id="style" runat="server" visible="false" />
It fools Visual Studio into thinking you've added a stylesheet to the page but it doesn't get rendered.
Here's an even more concise way to do this with multiple references;
<% if (false) { %>
<link rel="Stylesheet" type="text/css" href="Stylesheet.css" />
<script type="text/javascript" src="js/jquery-1.2.6.js" />
<% } %>
As seen in this blog post from Phil Haack.

Add the style on your usercontrol and import css in it.
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="WCReportCalendar.ascx.vb"
Inherits="Intra.WCReportCalender" %>
<style type='text/css'>
#import url("path of file.css");
// This is how i used jqueryui css
#import url("http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css");
</style>
your html

If you are creating composite UserControl, then you can set the CSSClass property on the child controls..
If not, then you need to expose properties that are either of the Style type, or (as I often do) string properties that apply CSS at the render type (i.e. take them properties and add a style attribute to the HTML tags when rendering).

You Can use CSS direct in userControl.
Use this in UserControl:
<head>
<title></title>
<style type="text/css">
.wrapper {
margin: 0 auto -142px;
/* the bottom margin is the negative value of the footer's height */
}
</style>
</head>
This will work.

Related

TinyMCE React strips out <style /> tag when placed inside the <head /> tag

I am using TinyMCE React with fullpage and code plugins enabled to create email templates. I am trying to insert a <style /> with custom css classes defined inside the <head />. But every time I reload the component, the <style /> is being stripped out. Here is a simplified exampled:
...
<head>
<style>
h1 {color:red;}
p {color:blue;}
</style>
</head>
Is this scenario supported? I have looked at a number of similar issues such as this. They seem to suggest the use of the valid_children option. As I understand, this option allows the inclusion of "invalid" tags inside parent tags. But <style /> inside <head /> is valid.
Any help would be greatly appreciated
Without knowing more about how you have TinyMCE configured it is very hard to say what is causing this issue. I am certainly able to load a style in the head of a document managed by TinyMCE as shown in this TinyMCE Fiddle:
http://fiddle.tinymce.com/cPgaab/2
How is your TinyMCE configured? If you update this fiddle to use your configuration is the style no longer kept?

Can I have a HEAD and BODY tag of a view independently from the _Layout.schtml page?

I have an ASP.NET MVC 3 project with a _Layout.cshtml and the css style sheet being accessed like this:
link href="../../Content/themes/base/jquery.ui.all.css" rel="Stylesheet" type="text/css"
This is fine for most of the views in my project but there is just one view that I would like to define the style sheet independently and in the view itself. Specifically in the head of the view. This means that the view would have to have it's own head and body tags independently of the _Layout.cshtml file.
Is this possible? If so can someone help me get started with it?
EDIT:
What I'm trying to do is put a style sheet in the head of this one view that would override the CSS from the _Layout.cshtml.
You could define an optional Razor Section in the head tag of your layout. Place it after your main stylesheet, and then it can be used in any views using the layout to bring in additional stylesheets that contain override rules.
_Layout.cshtml
<head>
<link href="../../Content/themes/base/jquery.ui.all.css" rel="Stylesheet" type="text/css">
#RenderSection("CssOverrides", false)
</head>
View.cshtml
#section CssOverrides {
<link href="../../Content/themes/base/jquery.ui.override.css" rel="Stylesheet" type="text/css">
<!-- additional stylesheets -->
}

Could not find CSS class selector message on content pages

I have two CSS files style1.css and style2.css. Each of them include many lines of styles.
I use them in all project pages. I have no problem with using them on Master page but, the content pages(the pages that inherited from master page) can't find the second CSS file!
For example on a inherited page when a div's class is set to a style from style2.css, it doesn't work. It can't find the CSS selector. :(
Master page's head tag:
<head runat="server">
<link rel="stylesheet" type="text/css" media="screen,projection,print" href="~/css/style1.css" />
<link rel="stylesheet" type="text/css" media="screen,projection,print" href="~/css/style2.css" />
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
Also, I've linked the CSS file on the inherited page but doesn't work.
Even, on the designing state the visual studio can't go to definition of the CSS selector line by pressing the F12 key on the desired selector name, so it raises "Could not find CSS class selector 'loginform'" error.
Do it declaratively in the master page by dragging the CSS files from Solution Explorer window to the code view of the mark-up.

ASP.NET MVC Inline Styling convert

Is there anyway I can get the all style of a page (even the style in some linked css files) as inline style?
For example, I have my css file:
body {
background-color: red;
color: black;
}
And this HTML:
<html>
<head>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>Hello World!</body>
</html>
So I'd like to get:
<html>
<head></head>
<body style="background-color: red; color: black;">Hello World!</body>
</html>
It would also work for me if I can get all the style in a style node.
from your comments, what you need is very different
What you are looking for is a tool that grabs an HTML page with it's own CSS styling and convert them into inline styling.
For that, plenty of tools are at your service:
http://premailer.dialect.ca/
There are more in Google, this is commonly used in Mailing as Email Client Applications do not intrepertate linked CSS but inline css.
If you are looking for a .NET solution, you might be interested in PreMailer.NET.
https://github.com/milkshakesoftware/PreMailer.Net
PreMailer pm = new PreMailer();
string premailedOutput = pm.MoveCssInline(htmlSource, false);
Old post, but I finally (2 years late!) got this up on github and in nuget:
https://github.com/lukeschafer/HtmlCleanser
Note: Premailer.Net (suggested by Arical) does not inline correctly.

CSS: Possible to define styles mid way through an html document?

In ASP.NET MVC, there are these snippets of html called view templates which appear when their matching data appears on the screen. For example, if you have a customer order and it has a vendor address, the vendor address view template shows up populated with data.
Unfortunately, these don't have access to "MasterPages" nor are aware of their CSS surroundings.
Instead of loading these up with style tags, is there any way to create partial CSS files that could work for that particular html snippet, a sort of in-line CSS style section?
It would be really nice to plop this down just before we render the partial view:
<style type="text/css">
input { margin: .2em .2em;
overflow: hidden;
width: 18.8em;
height: 1.6em;
border: 1px solid black;}
</style>
to have the 15 or so input fields in that particular Html snippet be formatted the same. These are swapped out, so the positions of the input fields change. This may also imply a CSS reset on each partial view.
I thing that an idea would be to include an additional content section in Head of the master page
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
</title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="CssContent" runat="server" />
</head>
In doing so, you can then inject the specific styles for each view.
Edit: After posting I think the above is not what you are referring to. What you may consider is rolling your own ViewUserControl similar to Script & CSS Registration Helper in ASP.NET MVC??
Why not have a css file that is referenced in the partial views or am I mising the point of the question?

Resources