15

Possible Duplicates:
What is the difference between <% %> and <%=%>?
C# MVC: What is the difference between <%# and <%=

I'm so confused with this.

Please explain the difference between this if possible..

What is the significance of that "=" there?

Edit:Thanks for all your answers.Please understand that It was hard to get any results by searching for "<%=" on google and on the search bar in stackoverflow as well.

Community
  • 1
  • 1
Josh
  • 13,120
  • 28
  • 112
  • 158

5 Answers5

35

<% %> is a generic code block.

<%= expression %> is equivalent to <% Response.Write(expression); %>.

mmx
  • 402,675
  • 87
  • 836
  • 780
9

Mehrdad's comment about <%$ piqued my curiosity, so I found this inline asp.net tags list by googling "asp.net inline code" (sans quotes). It has msdn links and descriptions of all the inline tags (<%, <%=, <%#, <%$, <%@, <%--).

Brian
  • 24,898
  • 16
  • 78
  • 165
8

It is confusing, and it takes a good deal of repetition to get comfortable with.

The <%= syntax is used for evaluating expressions whose returned values are intended to be included within HTML markup. For example:

<%= DateTime.Now.ToShortDateString() %>

This will include the current date in the HTML markup.

The <% is for inline statements, where you want to execute one or more commands at a specific point during the page rendering. I've used Html Helpers in the past by executing the helper method using <%. For example,

<% Html.TextBox("txtBox"); %>

Note that the statements used here have to be terminated with a semicolon in C# code.

EDIT: Removed erroneous details about Html helpers and void returns.

David Andres
  • 30,631
  • 7
  • 43
  • 36
3

<%= %> tag prints the output of the code in it, <% %> just runs the code.

pompo
  • 237
  • 1
  • 9
3

This post lists all the varieties nicely: ASP.NET "special" tags.

I would normally post this as a comment but there are a number of other dupes. I recall someone referring to them as "bee stings" (not official terminology) and the keywords I used to search for them were asp.net bee stings.

That said, here are some other dupes:

Community
  • 1
  • 1
Ahmad Mageed
  • 91,579
  • 18
  • 159
  • 169