DailyCoding > Web

The Script tag runat="server" Problem Solution Using ResolveUrl

Solution to the script tag runat="server" problem
Author admin on Jul 21, 2008 17 Comments
Rate it    (Rated 4 by 105 people)
17,095 Views

This might be an easy trick, but I ran into this issue a while ago; we can not specify the runat="server" attribute on a JavaScript file link as we do for images and style sheet.

<link id="mainSheet" runat="server" href="~/Images/styles.css" 
rel="stylesheet" type="text/css" />

By using runat=server the href of the link will automatically be resolved by the asp.net in we have placed ~/ in the path. This gives us advantages when we are linking this into a control and the control could be used by other pages places at a different directory level.

Same problem arise with using linked JavaScript. But this cannot be solved by using runat=server tag. Here is an alternative way to overcome this problem.

<script language="javascript" src="<%=ResolveUrl("~/App_Themes/MainTheme/jquery.js")%>" type="text/javascript"></script>

ASP.NET | Web
 

Discussion

Stefan On Oct 1, 2008 05:41 AM
Where is the solution??

Ramesh Soni On Oct 1, 2008 11:10 PM
@Stefan, sorry there was some problem with html code. It is corrected now. Thanks for reporting :).

kkkkkkkk On Feb 25, 2009 05:01 AM

Swati On Jun 8, 2009 06:25 AM
hi m new to asp.net
wanna knw as script tag is for client side then why we use runat attribute in it???

Jason Y On Jul 6, 2009 09:18 AM
Thanks for the post; it should solve the problem I am currently working through with #include'd files (instead of user controls)--though I might just replace #include'd files with user controls instead (good long term solution).

@Swati
The script tag is no longer for client side if you use runat="server" attribute, which is the very purpose for using it, and the very reason why that will _not_ work for resolving the "~/..." path in a script tag the way it works with other tags (<link /> in the example above)--Indeed, using runat="server" will resolve the "~/..." path, but will have the additional (undesirable) effect of making the script run on the server.

Either way, separate code files ("code-behinds) are generally preferred over <script runat="server">...</script>.

prawin On Jul 23, 2009 12:57 AM
Many thanks.. This solved my problem. Actually iam using URL rewriting..which has multiples keywords in the URL separated with a "/".

Relative paths will not work if you are using url rewriting..in that case i thought "~" would solve the problem but it didn't work for script tags..

prawin On Jul 23, 2009 12:57 AM
Many thanks.. This solved my problem. Actually iam using URL rewriting..which has multiples keywords in the URL separated with a "/".

Relative paths will not work if you are using url rewriting..in that case i thought "~" would solve the problem but it didn't work for script tags..

Prashant Atal On Aug 20, 2009 01:51 AM
I tried this but it only works in the Form tag and not in the Head tag :(.

renato On Oct 12, 2009 11:59 AM
I am working on my first ASP.Net project, and this just did the trick.
Thanks a lot!

John Holliday On Jan 6, 2010 04:21 PM
Prashant,

It works in the Head tag if you use <head runat="server">.

Derek On Jan 20, 2010 12:00 PM
I also found that prefacing the src attribute with HttpRuntime.AppDomainAppVirtualPath also solves the problem.

<code>
<script type="text/javascript" src="<%= HttpRuntime.AppDomainAppVirtualPath %>"/path_to_file.js"></script>
</code>

Derek On Jan 20, 2010 12:02 PM
Oops... I closed quotes after embedded asp tag... now it's fixed.


<script type="text/javascript" src="<%= HttpRuntime.AppDomainAppVirtualPath %>/path_to_file.js"></script>

Filip Urbanowicz On Feb 19, 2010 08:42 AM
Hi,
I have the same problem.

<head runat="server" id="Head" >
<title>to set</title>
<link href="~/obout/grid/styles/style/style.css" rel="stylesheet" media="all" type="text/css" />
<script language="javascript" type="text/javascript" src="~/Common/globalMain.js" ></script>
</head>

the ~ works with css links. But not with scripts !! why ?

I'v tried <%=ResolveUrl(...) %> -> Have an server error, does'nt work with head runat=server
I've tried HttpRuntime.AppDomainAppVirtualPath --> Same Error

Derek it does'nt work
Does anybody have a solution ?

Thanks a lot



Mahesh On Mar 3, 2010 03:09 AM
Hi you can added up the scripts in scriptmanager's to avoid the ur problem

Chris On Aug 17, 2011 08:30 PM
Hello maybe you can assist me with this.

I have an .aspx page that currently uses script to create a slider bar. The value of the slider bar is captured in the input element when the slider is changed. I'm trying to use server side scripts to pull the value from the input element "hvalue5." So, in turn I set the runat attribute to "server". Unfortunately, when this happens the javascript doesn't read hvalue5 as a defined element.

This client script works if the "runat=server" attribute is removed. Is there a better way to approach this?
<div class="slider" id="slider-5" tabindex="1" style= "width: 100%">
<input class="slider-input" id="slider-input-5" name="slider-input-5"/>
</div>
<input id="hvalue5" onchange="q.setValue(parseInt(this.value))" value="50" runat="server"/>
<script type="text/javascript">
var q = new Slider(document.getElementById("slider-5"),
document.getElementById("slider-input-5"));

q.onchange = function () {
document.getElementById("hvalue5").value = q.getValue();
};
q.setValue(50);
q.getMinimum(0);
q.getMaximum(100);
window.onresize = function () { q.recalculate(); }
</script>

Chris On Oct 10, 2011 02:11 PM
Filip - you should try '<%#ResolveUrl(...) %>' instead of <%=ResolveUrl(...) %>

Robert On Nov 29, 2011 12:25 PM
You may get an error using the 'ResolveUrl' approach : "The controls collection cannot be modified because the control contains code blocks. This post got me beyond this issue:

http://leedumond.com....ontains-code-blocks/

Leave a Comment

Name
Email Address
Web Site