Inline Expressions in ASP.NET


Inline expressions are used to write server side code directly on aspx, ascx, javascript pages or files.

Let’s discuss Inline expressions in ASP.NET

1. <% … %> embedded code blocks

They are supported in ASP.NET Web pages primarily to preserve backward compatibility with older ASP technology. It is NOT recommended to use embedded code blocks for complex programming logic, because when the code is mixed on the page with markup, it can be difficult to debug and maintain. Also the code is executed only during the page’s render phase

Consider following example, it shows an ASP.NET page with an embedded code block that displays the results of a for loop.


<%@ Page Language="C#" %></pre>
<form id="form1">
 <% for(int index=0;index<10;index++){%>
 <% Response.Write(index)%>
 <% }%>
 </form>
<pre>



2. <%= … %> displaying expression

It is an equivalent of the embedded code block. It is the simplest way to display information from an ASP.NET program such as a single string, an int variable, or a constant.

For example, the following sample code displays the current time:


<%@ Page Language="C#" %>

</pre>
<form id="form1">
 <%=DateTime.Now.ToString() %>
 </form>
<pre>



3. <%@ … %> directive expression

It is the syntax that specifies settings that are used by the page and by user control.

The @ Page directive can be used only in .aspx files, and the @ Control directive can be used only in .ascx files.

The ASP.NET page framework supports directives such as @ Page, @ Control, @ Register etc.

4. <%# … %> data-binding expression

It creates binding between a server control property and a datasource when the control’s DataBind method of this server control is called on the page.

e.g


<%@ Page Language="C#" %>
<script type="text/javascript">// <![CDATA[
 Protected string SayHello()
{
 Return "Hello World";
}
// ]]></script>

</pre>
<form id="form1"></form>
<pre>


5. <%$ … %> expression builder

The expression builder is used to set values of control properties based on the informationthat is contained in an application’s configuration or resource files. The following is the basic syntax of the expression builder:

<%$ Expression Prefix: Expression Value %>

The dollar sign ($) indicates to ASP.NET that the following expression is an expression builder. The expression prefix defines the kind of expression, such as AppSettings, ConnectionStrings, or Resources. Additionally, you can create and define your own expression builder. The expression value that follows the colon (:) is what ASP.NET will actually use as the value of a certain property.



 


The expression builder can be used in the ASP.NET Web Form page as:




6. <%– … — %> server-side comments block

It will allow developers to embed code comments in any location of the HTML source except for within <script> code blocks.

The following code example shows how to use the server-side comments block in an ASP.NET page:


<%@ Page Language="C#" %>

</pre>
<form id="form1">
 <%-- This is comment and wont be rendered

 --%>
 </form>
<pre>


Advertisements

2 thoughts on “Inline Expressions in ASP.NET

  1. Southern California Law Professional

    I really like your website.. comfortable colors & theme. Do you actually style and design this web site on your own and also would you actually bring in help to do it in your case? Plz respond as I!|m planning to layout my very own site along with want to understand the place you bought that out of. thanks a lot

    Reply
  2. Pingback: Introduction to ASP.NET Inline Expressions | ASPDOTNET-ANIL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s