In my CSS file I would like to have definitions such as:
.hr {
background:url('<%=CommonFunctions.AllocateStaticPath("/images/hr.png") %>');
width: 100%;
height: 2px;
margin: 40px 0;
}
This would be useful to me, as the path to each image differs on the production server and the development server and the function correctly sets the resources path. Being able to do this would simplify my publishing process.
How can I enable IIS7 to run ASP.net on CSS files? I've tried renaming the CSS file to .ashx and creating a rewrite rule but this seems to always 404.
This question inspired me to do some testing to see if I could somehow get the asked for syntax to work.
It turned out to be relatively easy. After a few different test versions, this is what I ended up doing.
Configuration
Create a brand new web application to test in
Create a folder called DynCss where the css files that need dynamic processing will be put
Register .css files to be handled by the Page handler for requests to this folder. For this I added the following to web.config:
<configuration>
<system.web>
...
<httpHandlers>
<add type="System.Web.UI.PageHandlerFactory" path="/DynCss/*.css" verb="GET"/>
</httpHandlers>
...
</system.web>
</configuration>
Register a build handler for .css files:
<configuration>
<system.web>
...
<compilation debug="true" targetFramework="4.0">
<buildProviders>
<add extension=".css" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>
</compilation>
...
</system.web>
</configuration>
After doing these changes, I can proceed to testing it.
Testing
For the purpose of testing I added DynamicStyles.css to the DynCss folder. Contents of DynamicStyles.css:
<%# Page Title="DynamicStyles.css" Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType="text/css";
Response.Cache.SetCacheability(HttpCacheability.Public);
}
</script>
body {
font-weight: <%= TestDynamicCss.Code.Constants.FontWeight %>;
}
Note: The TestDynamicCss.Code.Constants.FontWeight referrs to a static property on a static class. I simply returns the string "bold".
Finally, I link to it in Default.aspx:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="TestDynamicCss._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<link href="/DynCss/DynamicStyles.css" rel="Stylesheet" type="text/css" />
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET! Is this now bold? Why yes, it is!
</h2>
</asp:Content>
Comments
Using this approach you get the behaviour that you asked for. The drawbacks are that you don't get any automatic cache handling as you do for static css files. Also, this approach (as far as I can tell) makes it impossible to use the Css bundling features of Asp.Net 4.5. Also (needless to say) you don't get C# intellisense when coding in the Css file.
Try this:
background:url('<%= CommonFunctions.AllocateStaticPath("/images/hr.png") %>');
Can't you just create an ASPX page that returns CSS instead of HTML? Then reference the .aspx file in your CSS declaration.
I think you can use the ashx handler a page and add the response header:
Content-Type: text/css
Related
I have started learning asp.net. I went through basics and now i am started to build small application.
I am using VS 2012 and created Empty Web Application Project with VB.
I can see web.config created automatically and following are the line written in it :
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
I created Default.aspx file and wrote following lines of code :
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" %>
<%
HelloWorldLabel.Text = "Hello, world!";
%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label runat="server" id="HelloWorldLabel"></asp:Label>
</div>
</form>
</body>
</html>
When I am running this application on browsers, I am getting following error that page :
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30037: Character is not valid.
Source Error:
Line 2:
Line 3: <%
Line 4: HelloWorldLabel.Text = "Hello, world!";
Line 5: %>
Line 6:
Source File: c:\users\anjum.banaras\documents\visual studio 2012\Projects\Students\Students\Default.aspx Line: 4
Can any one help me on this ? I am just beginner on asp.net. Your help can save lots of my time.
Thanking you in advance !!
You've set the programming language of the page to VB (Visual Basic), but the line it is complaining about is written in C# syntax. Either change the line to be valid VB code:
HelloWorldLabel.Text = "Hello, world!"
(I think that removing the ; is all that's needed, but I never code VB so I'm not sure)
or change the page language to C#:
<%# Page Language="c#" AutoEventWireup="false" CodeBehind="Default.aspx.vb" %>
I was getting this error since my designer file was missing from the solution (I don't know how,seriously). So try adding a designer file for the aspx file in the solution; it worked for me.
I copied my code to another editor (notepad++) and was able to see the problematic chars. After i removed them, the code worked again.
��myClass.myArray(28) = "myFirstValue"
��myClass.myArray(29) = "myValue"
Is there any easy way to save users position on the site after postback ?
using maintainScrollPositionOnPostBack="true" in my web.config pages section doesnt work.
I mean it does work but only in ie. Is there any way to make it work on firefox and chrome ?
thanks for any suggestions
Koder Gurl had the same type of problem back in 2010. She posted the code that was giving her trouble and from the sounds of it, she had the browser issues you are having.
Try looking at her solution at this page: http://www.kodergurl.com/2010/08/maintain-scroll-position-on-post-back.html
I hope that will help you.
This can be done in different ways as below:
a. Set MaintainScrollPositionOnPostback property to true in Page directive for specific page(s).
<%# Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default"
MaintainScrollPositionOnPostback="true" %>
b. For whole site, add below section to web.config file.
<configuration>
<system.web>
<pages maintainScrollPositionOnPostBack="true" />
</system.web>
</configuration>
I have problem in using one user control in another user control in C#.
This is the code.
<%# Control Language="C#" AutoEventWireup="true" CodeFile="CustomerDetail.ascx.cs"
Inherits="Controls_CustomerDetail" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<%# Register Src="~/PL_Controls/Admin/AddAddress.ascx" TagPrefix="CustomerAddress" TagName="Address" %>
<%--<%# Register Src="~/PL_Controls/Admin/AddAddress.ascx" TagPrefix="UC" TagName="AddAddress" %>--%>
<style type="text/css">
.overlay_style
{
background: #777777;
opacity: 0.7;
filter: alpha(opacity=70);
}
</style>
<div runat="server" id="divaddress"
style="border-style: dashed; background: golden; background-color: #C0C0C0; display: block;">
<CustomerAddress:Address Id="CustomerAddress" runat="server" />
</div>
This shows green line below "Address: in div tag with message that Address is not a non element,. This can occur if there is compilation error in web site or web.config file is missing. But I have web.config in my application. When I try to use this user control in server side code using its ID, it is not available at server side.
Please help me to get rid our of these problem...
Try changing the Id to something other than CustomerAddress - you probably are running into a name collision issue.
Make sure that the designer.cs file declares the user control instance. Also, if the user controls are in the same assembly, and you're referencing them via web.config, that could cause problems as well.
Manually declare CustomerAddress in your designer.cs file then build your project. You will see a more related error for this problem.
I am getting the error below when trying to build the web site project in Visual Studio 2010:
The page '/WebSite/controls/C2.ascx' cannot use the user control '/WebSite/controls/C1.ascx', because it is registered in web.config and lives in the same directory as the page.
I have 2 web user controls:
controls/C1.ascx
controls/C2.ascx
The controls have been registered in web.config:
<configuration>
<system.web>
<pages>
<controls>
<add src="~/controls/C1.ascx" tagPrefix="my" tagName="C1"/>
<add src="~/controls/C2.ascx" tagPrefix="my" tagName="C2"/>
</controls>
</pages>
</system.web>
</configuration>
C1.ascx contains just a static HTML, C2.ascx is trying to include C1:
C1.ascx contains just some plain static simple HTML. C2.ascx is trying to include C1.ascx:
<%# Control Language="VB" %>
<my:C1 runat="server" />
<p>Hello from C2</p>
When trying to build the project, I am getting the error message at the top. I realise this issue can be fixed by adding another Register directive to C2.ascx...:
<%# Register Src="~/controls/C1.ascx" TagPrefix="ctl" TagName="C1" %>
...but I'm wondering if there's a cleaner solution and why am I getting the error in the first place?
Your only possible solutions are to:
Move the control out of the directory its currently sharing with outer.ascx, or
Re-register the control inside of the outer.ascx like you already mentioned
Re-write them in code as controls in a separate library
I personally think moving is the easiest, if it will work for your solutions. Second would be re-registering, even though annoying. Abstracting them out into a full code library is probably not worth the effort if this is the only reason you are doing it.
You could also put the controls into different folders. But I don't think this is much cleaner or better.
BTW: this behavior is by design, as you can read on this MSDN page (look for the yellow note almost at the end of the page).
I am feeling like an idiot right now, but I cannot for the life of me figure out why I cannot seem to call user controls from within my asp.net webpage. I'm still learning asp.net but I can't find any information from searching on google.
I'm trying to load a specific control on the page when the user presses a linkbutton. So I created an empty user control via the right click menu:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
In other words, I have not touched any part of the created user control. Yet attempting to create this web control and add it to the form seems to not work, as it claims that the WebUserControl class does not exist (I have no other controls in my project):
UserControl blah = new WebUserControls();
produces a "The type or namespace is invalid". Why can none of my asp.net webform pages get the control into scope?
The new control must be added to the site's Web.config file.
<configuration>
<system.web>
<pages>
<controls>
<add tagPrefix="my"
tagName="WebUserControl"
src="~/WebUserControl.ascx"/>
</controls>
</pages>
</system.web>
</configuration>
Use this to place the new control in an .aspx page.
<my:WebUserControl runat="server" ID="MyWebUserControl" />
In addition to registering them one-by-one in web.config, you can also use a <%# Register %> directive in the source of the markup files that reference the control.
Or, you can move your controls into a DLL and include them all by referencing the assembly from your web.config (takes some extra work, though).