11

I am getting the error on the below code in asp.net 4.0

<script type="text/javascript" src='<%#=ResolveUrl("~/Scripts/jquery-1.4.1.js")%>'></script>

Error Message: CS1525: Invalid expression term '='

I am using this code in Site.Master in head tag

rsbarro
  • 27,021
  • 9
  • 71
  • 75
Raj Kumar
  • 6,970
  • 5
  • 30
  • 40

2 Answers2

34

You can't use <%# and <%= at the same time. Try it like this:

<script type="text/javascript" src='<%= ResolveUrl("~/Scripts/jquery-1.4.1.js")%>'></script>

EDIT
If you are getting an error that states:

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

when you try to use <%= ResolveUrl(..., it is because something in your code is attempting to add controls to your header control in Site.Master. If that is the case, switch the script tag to read:

<script type="text/javascript" src='<%# ResolveUrl("~/Scripts/jquery-1.4.1.js")%>'></script>

and make sure you call the DataBind() method on the header tag at some point (for example, from the Page_Load method for Site.Master):

public partial class SiteMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Header.DataBind();
    }
}
rsbarro
  • 27,021
  • 9
  • 71
  • 75
  • 3
    Beat me to it :) Also, if you need to use # then its just <%#Resolve, but you need to ensure that Page.DataBind(); is called somewhere. – Russ Clarke Jul 18 '11 at 12:00
  • Hi got this error "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)" – Raj Kumar Jul 18 '11 at 12:01
0

You can use ResolveUrl with Eval like this. No external code needed.

<img src='<%# ResolveUrl(Eval("FILE_URL").ToString()) %>' alt=""
     style="width:50px;height:50px"/>
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47