4

Is it possible to do things in a PHPish way in ASP.Net? I've seen <%= %> but I've tried it and couldn't get it to work.

The PHPish equivalent of what I want to do is

<script src="<?php echo ResolveUrl("jquery/js/jquery.js"); ?>"></script>
Earlz
  • 62,085
  • 98
  • 303
  • 499
  • why you can't use the absolute path? because you your localhost virtual directory vs no virtual directory on live environment? – Claudio Redi Jun 01 '10 at 23:30
  • You really want to use this with caution though as "best practices" usually say to keep your UI and logic separate as possible. – Daisetsu Jun 01 '10 at 23:31
  • @Dai yes I know but adding a literal control and modifying it in the code-behind seems really dirty. @Claudio. I ran into a problem where in development our virtual path is `/` and in production our path is `/product` – Earlz Jun 01 '10 at 23:33

1 Answers1

7

Yes, it's quite possible. You should familiarize yourself with all the variations of the (so called) alligator tags though.

Put code in between <% %> blocks. The <%= %> variant is a shortcut for Response.Write, and is used as a shortcut for directly outputting a variable to the page.

The following should work, as long as ResolveUrl is returning a string. Notice there is no ";" to end the line though.

<script src="<%= ResolveUrl("jquery/js/jquery.js") %>"></script>
womp
  • 115,835
  • 26
  • 236
  • 269
  • Just to make a note, I later changed my code to use `<%# %>` instead because of this issue http://stackoverflow.com/questions/778952/the-controls-collection-cannot-be-modified-because-the-control-contains-code-blo – Earlz Jun 02 '10 at 00:25
  • 4
    Don't forget <%: %> in asp.net 4.0 for HtmlEncoding – Aren Jun 02 '10 at 00:26