Joe.wang
Joe.wang

Reputation: 11791

What is difference between <%=%> and <%#%>

All, As for the inline expression in asp.net ,I had some questions about them.

  1. What is the difference between them ?
  2. In what situation ,Which one is better?

By the way, I found below code the first one doesn't works , but second one works. Why?

<head runat="server">
    <title></title>
    <script src="<%#FullyQualifiedApplicationPath%>Scripts/jquery.js" type="text/javascript"></script>
</head><!--Not OK-->

<head runat="server">
    <title></title>
    <script src="<%=FullyQualifiedApplicationPath%>Scripts/jquery.js" type="text/javascript"></script>
</head><!--OK-->

FullyQualifiedApplicationPath is a variable defined in code behind. Thanks your review.

Upvotes: 2

Views: 116

Answers (4)

Rohit Paul
Rohit Paul

Reputation: 336

<%#%> is used to bind some data or control by using BIND(read and write purpose) or EVAL(for readonly purpose).

Where as <%=%> is used to display the value of a session in a html page.

Upvotes: 2

Ashish Babu
Ashish Babu

Reputation: 1175

For example when u w3ant to bind data to a grid we use the following syntax

     <ItemTemplate>
       <asp:Label ID="lbl_Id" Text='<%# Bind("Id") %>' runat="server"></asp:Label>
     </ItemTemplate>

The above syntax is used to bind the data in the gridview

while the <%=%> You will be able to do c# coding in default. aspx page as well, here is the sample code.

To show some output on the page and do some coding over out there


  <%=Session["value"].ToString() %>

It will load the session value in the default page.

Upvotes: 1

Jonas Stawski
Jonas Stawski

Reputation: 6752

<%=%> is the equivalent of Response.Write or for writing directly to the page (response), while <%# %> is used specifically for databinding.

Upvotes: 2

Dan Drews
Dan Drews

Reputation: 1976

<%= %> is for loading variables <%# %> is for databaind

Consult this post or this question for more info

Upvotes: 6

Related Questions