troubleshooting Question

Link CSS, js and other files using relative paths ASP.NET

Avatar of Dermofit
Dermofit asked on
5 Comments1 Solution619 ViewsLast Modified:
I have developed a User Control for my application. It references a css file and a js file. I want it to be self-contained, that way if I want to include it in a page I would just write the reference to it, like this:

<%@ Register TagPrefix="mc" TagName="MyControl" 
   Src="Controls/Path/MyControl.ascx" %>

Right now, the way I'm handling it is by assuming that all the pages are located at root level, so this is how my User Control code looks like:

<%@ Control Language="C#" AutoEventWireup="true" 
   CodeFile="MyControl.ascx.cs" Inherits="Controls_Path_MyControl" %>
<link href="Controls/Path/MyControl.css" 
   rel="stylesheet" type="text/css" />
<script src="Controls/Path/MyControl.js" 

... content ...

And that works if I place the component in a page located at root dir. But I'm wondering if there's a more elegant way to do it.

I tried using the runat="server" so I could use paths relative to the Control but when I do it, all of the sudden I get 21 errors on my javascript. Errors that were not there when using the full reference. I stripped down my java script all the way to the following code, and I still get a "identifier expected" error.

function OnMouseOver(e) {alert("Hello");}

Any ideas?

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros