Sunday, March 31, 2013

The difference between <%= and <%# and <%$ in ASP.NET

Sign '<%=' & '<%#'  :


 <%= expression %> and <%# expression %> in ASP.NET. It seems like both work in a lot of cases, but in other cases, only the # (data binding) version works. 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <p>Equals: <%= this.TestValue %></p>
        <p>Pound: <%# this.TestValue %></p>
        <p>Equals label: <asp:Label runat="server" ID="_equals" Text="<%= this.TestValue %>" /></p>
        <p>Pound label: <asp:Label runat="server" ID="_pound" Text="<%# this.TestValue %>" /></p>
    </div>
    </form>
</body>
</html>
And the code behind is:
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        _testValue = "2";
    }

    protected void Page_PreRenderComplete(object sender, EventArgs e)
    {
        // DataBind();
        _testValue = "3";
    }

    public string TestValue
    {
        get { return _testValue; }
    }

    private string _testValue = "1";
}
Here's what the result is when the DataBind() line is commented out:
Equals: 3
Pound:
Equals label:
Pound label:
And, when it's not commented out:
Equals: 3
Pound: 2
Equals label:
Pound label: 2
At first glance it looks like the Equals label case did nothing. But, if you view source, you see:
<p>Equals label: <span id="_equals"><%= this.TestValue %></span></p>
The literal expression made it down to the browser and it's just invalid HTML. What you can see as a result is:
  • The <%= expressions are evaluated at render time
  • The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
  • <%# expressions can be used as properties in server-side controls. <%= expressions cannot.

Sign : <%$


The basic syntax of an ASP.NET expression is the following:
<%$ expressionPrefix: expressionValue %>
The dollar sign ($) indicates to ASP.NET that an expression follows. The expression prefix defines the type of expression, such asAppSettingsConnectionStrings, or Resources. Following the colon (:) is the actual expression value that ASP.NET will resolve.
Expression syntax is not bound to any specific .NET language. You can use the same expression syntax whether you use Visual Basic, C#, or any other programming language in your ASP.NET pages.
Connection Strings


A common use of expressions is to set the connection string property of a control, such as the SqlDataSource control, based on the value of a connection string stored in the Web.config file. For example, you might have the following SqlDataSourcecontrol with a connection string attribute:
<asp:SqlDataSource ID="SqlDataSource1" Runat="server" 
    SelectCommand="SELECT * FROM [Employees]"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>">
</asp:SqlDataSource>
The highlighted code shows an expression, which is contained within the quotation marks and denotes the attribute's value. The expression references a connection string named "NorthwindConnectionString1" that is defined in the connectionStringselement of the Web.config file. The connectionStrings element might look like the following:
<configuration>
  <connectionStrings>
    <add name="NorthwindConnectionString1" 
      connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
    <!-- additional settings -->
</configuration>

No comments:

Post a Comment