Reference to styles and scripts in Master.Page using Resolveurl

Asked

Viewed 362 times

0

In Master.Page I make references to styles and scripts that will be inherited by other pages through Resolveurl.

<script src="<%# ResolveUrl("~/") %>Scripts/jquery-1.11.1.js" type='text/javascript'></script>
<script src="<%# ResolveUrl("~/") %>Scripts/jQuery-Mask-Plugin.js" type='text/javascript'></script>
<script src="<%# ResolveUrl("~/") %>Scripts/jQuery-Formatacao.js" type='text/javascript'></script>

<link href="<%# ResolveUrl("~/") %>Styles/estilo.css" rel='stylesheet' type='text/css' />

I saw some posts that suggest to insert the code below so that the initial reference can be effective:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    Page.Header.DataBind();
}

This is really necessary?

I’m using framework 4.0.

1 answer

2


The tags <%# %> are used to data linkage, and therefore it is necessary that the code execute the binding (DataBind()) in case it is not done automatically (as in the data binding controls). You can choose to use tags <%: %>, without the need to use the second code block that is in the statement of your question.

In case, the code would look like this:

<script src="<%: ResolveUrl("~/Scripts/jquery-1.11.1.js") %>" type='text/javascript'></script>
<script src="<%: ResolveUrl("~/Scripts/jQuery-Mask-Plugin.js") %>" type='text/javascript'></script>
<script src="<%: ResolveUrl("~/Scripts/jQuery-Formatacao.js") %>" type='text/javascript'></script>

<link href="<%: ResolveUrl("~/Styles/estilo.css") %>" rel='stylesheet' type='text/css' />

In case of future doubts, here is the explanation of how the ASP.NET tags work:

<% %> defines a server code block that is executed during page rendering. You can execute statements and call methods from the page class the block is inserted in (http://msdn.microsoft.com/en-us/library/ms178135%28v=vs.100%29.aspx).

<%= %> functions as a Response.Write(), most useful for displaying unique information (http://msdn.microsoft.com/en-us/library/6dwsdcf5%28VS.71%29.aspx).

<%# %> defines a data binding expression (http://msdn.microsoft.com/en-us/library/ms178366%28v=vs.100%29.aspx).

<%$ %> defines an ASP.NET expression (http://msdn.microsoft.com/en-us/library/d5bd1tad%28v=vs.100%29.aspx).

<%@ %> defines a directive expression (http://msdn.microsoft.com/en-us/library/xz702w3e%28VS.80%29.aspx).

<%-- --%> sets a block of comments on the server side (http://msdn.microsoft.com/en-us/library/vstudio/4acf8afk%28v=vs.100%29.aspx).

<%: %> works like the <%= %>, but encodes the output in HTML (http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2).

You can check more details of these expressions in: http://support.microsoft.com/kb/976112

Reference

Based on mbanavige user response in ASP.NET forums.
<URL: http://forums.asp.net/t/1139381.aspx>

  • Thank you very much, very enlightening the post. In the example you used the "#" for <%# Resolveurl("~/Scripts/jquery-1.11.1.js") and the ":" for the others, it was some mistake or has to be anyway?

  • Oops, it was a mistake. I’m sorry.

  • Thank you again!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.