ASP.Net Page Syntax Code Blocks

ASP.Net Page Syntax Code Blocks

The <%# %> is used for data binding. This data binding expression creates a binding between a property on an ASP.NET page and a data source when the DataBind method is called. Data binding expressions can be included on the value side of an attribute/value pair in a server control or anywhere on the page. All data binding expressions on a page must be put between the <%# and %> characters. Here would be an example of using a data binding expression to bind to a server control.

<form runat="server">
    <asp:DropDownList id="List" runat="server">
    <asp:button Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>
        Selected Name: <asp:label text='<%# List.SelectedItem.Text %>' runat="server"/>

The <%= %> symbols are used to display information. The value that gets entered in between the symbols will be written out to the current page. These symbols will be executed and displayed when they appear on the page. So a quick example would be having a variable named Transportation. Let’s say the value of that variable is equal to Truck. With that in mind, let’s look at how this would be used in the mark up.

I drive a <%= Transportation %>! //Code
I drive a Truck! //Output

The <% %> symbols are used for Embedded code blocks. The embedded code block is server code and is executed during the pages render. The block can be used to execute programming statements or call functions in the current page class. Here is a simple example from msdn. You’ll see how it uses the <% %> to encase the for loop that will then execute when the page renders.

    <form id="form1" runat="server">
        <% for(int i = 0; i < 6; i++) %>
        <% { Response.Write("<br>" + i.ToString()); }%>

The <%– –%> is simply used to indicate a server side comment. These can be used anywhere within the page but cannot be used within the <script> tag. You also may not used a server side comment within an embedded code block. So if you have a <%– –%> inside of a <% %> it will result in a compilation error.

    <asp:button runat="server" id="MyButton"
    OnClick="MyButton_Click" />

The <%@ %> is used to used to signify a page directive. ASP.Net includes many different directives which are as follows. Taken right from msdn.

@ Page Defines page-specific attributes used by the ASP.NET page parser and compiler. Can be included only in .aspx files.
@ Control Defines control-specific attributes used by the ASP.NET page parser and compiler. Can be included only in .ascx files (user controls).
@ Import Explicitly imports a namespace into a page or user control.
@ Implements Declaratively indicates that a page or user control implements a specified .NET Framework interface.
@ Register Associates aliases with namespaces and class names, thereby allowing user controls and custom server controls to be rendered when included in a requested page or user control.
@ Assembly Links an assembly to the current page during compilation, making all the assembly's classes and interfaces available for use on the page.
@ Master Identifies an ASP.NET master page.
@ WebHandler Identifies an ASP.NET IHttpHandler page.
@ PreviousPageType Provides the means to get strong typing against the previous page as accessed through the PreviousPage property.
@ MasterType Assigns a class name to the Master property of an ASP.NET page, so that the page can get strongly typed references to members of the master page.
@ OutputCache Declaratively controls the output caching policies of a page or user control.
@ Reference Declaratively links a page or user control to the current page or user control.

It is important to note that if you do not specify one, the default then becomes the @ Page directive.

The <%: %> symbols were introduced in ASP.NET 4 and it works much like the <%= %> but it will also automatically HTML encode the output. In the example below, you will notice that the second one does the encoding for you and helps save some space.

<div id="OldWay">
    <%= Server.HtmlEncode(Model.Content) %>
<div id="NewdWay">
    <%: Model.Content %>

I got tired of trying to look them up every time so I decided to consolidate them into one place for quick reference.

Leave a Reply

Your email address will not be published. Required fields are marked *