Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ResolveUrl not working inline

Tags:

asp.net

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

like image 673
Raj Kumar Avatar asked Jul 18 '11 11:07

Raj Kumar


1 Answers

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();
    }
}
like image 60
rsbarro Avatar answered Jan 01 '23 14:01

rsbarro