Best way to inject server-side variables/function results into CSS under ASP.NET? - asp.net

My ASP.NET app has some server-side methods whose results I'd like to inject into my CSS files. For example, rather than hard-code the URL to a logo, I might like to insert a call to MyHelperClass.GetCurrentLogoUrl() in the middle of the CSS file.
If I were writing an ASPX page, I could use code render blocks x (that is, stuff like "<%=MyHelperClass.GetCurrentLogoUrl()%>") in the middle of my HTML markup. It would be nice to do something similar for CSS.
There are some CSS preprocessing frameworks, e.g. dotless, and they seem to have some cool features, but I'm not aware of any of them supporting making C# calls like this.

You could just make an aspx page and link to it when including a stylesheet. You'll miss out on css highlighting though.
<link href="http://domain.com/css.aspx" type="text/css" rel="stylesheet">
This has the potential to generate unnecessary overhead though, especially if most properties you're modifying do not change at runtime. In that case you may want to research TT files which basically generate other files when deployed - in your case, you could have it generate the css with whatever complex logic.

David P's answer to ASP.NET MVC URL auto-resolution in CSS files suggests one approach. If you give your CSS files the .aspx extension and then use a Page directive to tell ASP.NET that it's actually a CSS file, like so
<%# Page Language="C#" ContentType="text/css" %>
then you can write CSS, but also use the normal ASP.NET code render blocks. It feels a little bizarre to name CSS files .aspx, but an initial test suggests this could work.
UPDATE: Props to o.v. for pointing this out even faster than me.

You can inject a classname into the html, something like:
<input type="text" id="myTextBox" cssClass="<%=myClassName%>" />
You can also change the class name in the code behind using the .attributes method.

Add the following into your <head></head> section of your page
<style runat="server" id="cssPhoneResize" type="text/css"></style>
Then from your server-side method:
string logoUrl = "/images/logo2.jpg";
cssPhoneResize.InnerHtml += "#Image{-image: url(" + logoUrl + ");}";
Hope this helps!

Related

DotNetNuke/Evoq - add custom stylesheets just before closing head

I'm trying to add two custom css stylesheets just before the closing </head> without breaking skin and/or hacking skin?
I built two css files to turn site into responsive design without actually touching any of the skin/template code so it's necessary to have in last ordering of css files.
Your best best is to register it using the DNN CSS Include skin object, adding it to your skin and simply setting a priority that is high.
<dnn:DnnCssInclude runat="server" FilePath="~/Your-PathHere" Priority="100" />
You just need to be sure that you have the following register in your skin as well.
<%# Register TagPrefix="dnn" Namespace="DotNetNuke.Web.Client.ClientResourceManagement" Assembly="DotNetNuke.Web.Client" %>
The full API Details for Client Resource Management will help. Highest DNN priority appears to be 35

Displaying html page in ASPX page

I need to display dynamic created html in aspx(server side html);
I tried to use Iframe but it will not displaying anything ; it will not work because of security reasons ;
Is there is any controls that will display html page? Dynamic html have its on css and javascripts so I can’t use html text box controls.
If anyone have solution please help
Thanks
Since your dynamic page has its own CSS & Javascript, I'm assuming it's not written to coexist with its host page. I'm also assuming that when you tried to use the iFrame you just tried to write straight to it from the containing page.
I would suggest moving your code that generates the HTML to a separate ASPX page and referencing that page as the source of your iFrame or rewriting your CSS & Javascript so it will coexist and using a DIV.
Also, it's sort of hard to come up with a workable solution without you showing some of the code you have currently.
Have a look at the
<asp:Literal>
control. There's an example here: Set ASP Literal text with Javascript
-- EDITED 03/05/2012 --
Simple example of an asp.net literal control in action:
.aspx code
<asp:Literal ID="MyLiteral" runat="server" />
.vb code behind
Dim k As String
k = "<table style=""border: 1px solid red;""><tr><td>Cell 1</td></tr><tr><td>Cell 2</td></tr></table>"
MyLiteral.Text = k
If I compile this in VS2008 I get a two row table with a red border in IE.
I found the answer!! Use UFRAME!! It's simple and easy!! uframe.codeplex.com

How to use jQuery on my page?

I'm following this tutorial but it doesn't tell me how to run this jQuery script. Since this script will be run pretty much everywhere, I should attach this script to the Masterpage right, but how?
I guess what I'm asking is, what HTML tag do I need to reference the jQuery script, and where to put the jQuery code.
I have this library already in my project:
Thanks.
jQuery is a client-side scripting tool. ASP .Net is a server-side language.
You are correct, to add a reference to jQuery for all pages it is a good idea to use a master page for this purpose.
In the master page, you simply add the HTML script reference to the master page:
<# MasterPage .... >
<html>
<head>
<script src="../Scripts/jquery-1.4.1.js"></script>
</head>
<body>
<asp:ContentPlaceHolder id="Content" />
</body>
</html>
Add a script element for each of the jQuery scripts. Make sure the jquery-1.4.1.js is the first referenced though.
Also make sure you use the <script></script> instead of <script/> due to some browser issues.
Some newer Visual Studio MVC project files do this script referencing for you (as Lenial mentioned), and this may be easier.

Simple HTML construction in ASP.NET?

A simple question, I think:
I want to put a tag into an ASP.NET app I've been asked to maintain, so I'm coming at this from a newbie point of view just tinkering around the edges without knowing a lot.
I wrote an old ASP application back in 1998, so I am just running on memory...
How do I write some output to the webpage?
I know I can use an
<asp:label id="blah">
but then I need to define a Label blah; in my code behind and then assign it.
I believe that I can put in-place:
<% Response.Write("sometext"); %>
and that will write sometext in the location within the page. (Am I correct?)
Lastly, I remember there was a syntax to the effect of
<%= "some string" %>
but I can't find the documentation on it, to say either it is deprecated, unadvised, or the rationale for such a decision.
I have tried googling for "ASP.NET grammar" but I can't even find a good description that "<%=" even exists, though it is mentioned in a few blogs.
For something simple, like inject the global version number, or the current date, then I can't see anything particularly wrong with in-place composition - it would save me defining 15 labels and having to initialise them all - though perhaps the asp:label approach could reference one global instance of a label?
Just asking for opinions on good practices :)
<%= string %> is perfectly valid ASP.NET syntax. The reason you will often find references to problems with using that is people use <%= (equivalent to Response.Write) when they should use <%# (for databinding) or vice-versa.
For example, we use it very extensively in our content managed site, where we pull in values from a global settings repository:
<%= SiteContext.Current.GetSetting("SiteTitle") %>
MSDN:
MSDN entry on <%= (this is under the JScript.NET section but still applies)
MSDN entry on <%#
Some others suggest <%= is not a "best practice" or a very good approach, but I strongly disagree with that sentiment. For an MVC-ish type site (especially a site that is template- or view-driven in some way), the most direct approach is frequently more effective than using server controls.
Just be mindful that when you use an <asp:Label /> it renders the .Text inside the <span> tag whereas an <asp:Literal /> adds no extraneous HTML to the string passed to it.
For example, if you were building a content management system and wanted to display user-driven HTML, a Label control would not correctly display the output from a WYSIWYG type rich textbox whereas a Literal control is the appropriate choice.
The <%= %> is the late-bound equivalent of the Literal's .Text property. The only difference here is when the value is placed in the page (aside from obvious syntax and separation of concerns paradigm) during the course of the page lifecycle.
Since the .Text property is on a control inherited from WebControl, it can be set/read/manipulated during any of the events following the control's Load event (wherever/whenever you load the control inside the page), but the <%= %> text cannot be directly read/used/manipulated by the code-behind without referencing some other control to get to it (like a containing div's InnerHtml property).
There are lots of options. You could use a single label, and string concatenate all the data you want displayed in that location.
You could create a user control with the layout you want and assign values that way.
You could inject it directly with response.write or the <%= %> syntax
You could create an HtmlGenericControl in your code behind (it's a div), add some text to it, and inject it into the pages controls collection.
Whatever you pick, try and go with the existing style of the coded page.
Look up the term "render blocks" for the <% %> syntax.
How about using
<asp:Literal id="z" text="goofy" runat="server" />?
Labels are usually used with forms.
You can also take full control of the rendering of your pages and controls and compose whatever you need to. You control the HTML, the order of rendering your controls, etc...
Go with the <asp:label /> (or a literal control if you want to customize some html in the content). Seriously. I'ts not that hard: when you put label in your markup visual studio will create it in the code-behind for you, so there's no extra work involved.
You could use the <%= "some string" %> syntax in web forms, but there can be issues when mixing that with the asp controls and there's a good reason new frameworks moved away from mixing logic like that in with your markup.

ASP.NET MVC - CSS Class in a ViewUserControl - The class or CssClass is not defined

I have a <table/> in a ViewUserControl that I have given the tag a class="tblRaised" attribute/value. Visual Studio keeps underlying tblRaised and telling me - The class or CssClass is not defined.
Why is the intellisense engine trying to validate my CSS class names here? Anyone else run into this? Is this a bug? How would intellisense even know where my css file was in a ViewUserControl?
Because the styles are usually included in your view or master page, VS can't find them in your ViewUserControl. If you add something like the following to your ViewUserControl you can get around the issue (and get intellisense) without including the CSS twice.
<% if (false) { %>
<link rel="stylesheet" type="text/css" ...
<% } %>
This will get the intellisense since it can find the stylesheet, but the use of if (false) actually prevents it from being included at runtime.
Typically in the ASP.NET world (not MVC) you'd specify your styles in the master page or your current page. VS then reads all the style information and then tries to help with intellisense to output the class names from your styles onto your aspx page while typing. With MVC it is trying to do the same thing, but it's probably just not finding your files, and throwing up a warning.
Just ignore it for now, I'm sure they are going to try and support that with the 1.0 release.
It's a known bug. Visual Studio IntelliSense is being too helpful. :)
Use this workaround in your user control markup files, it will make VS IntelliSense happy:
<% if (false) { %><link href="../../Content/Css/MyCssDefinitions.css" rel="Stylesheet" type="text/css" /><% } %>

Resources