Skip to main content

A quick reference for the ASP.NET inline server tags. Great for beginners!

Here is a complete guide of the ASP.NET inline tags. I am sure you will find it useful!

Posted in Tutorials, ASP.NET

I have to tell you: Sometimes I get confused on which server-side tag I should use to make my codes. Normal since I never use all of them so oftem. If you suffer from the same issue this text might be useful as a quick reference.

<% ... %>

The most common of the inline tags. It's used when you need to make some small code on the aspx page or render some expression there. For example:

<% if(isAdmin) { %>
Hello Admin!
<% } %>

<%= ... %>

This is the simplest way to display some information in an ASP.NET page. The equal sign acts as Response.Write rendering the object directly on the page. For example:

Today is <%= DateTime.Now %>

<%: %>

This was implemented in ASP.NET 4 and it's basically the same as above but with one small thing: This server tag automatically does the HTML Encoding for you. So, instead of using Server.HtmlEncode(htmlText) on your code you can simply use as the example:

Here is some html <%: htmlText %>

<%-- ... --%>

This tag is a server-side comment. Since this code is not rendered on the browser you can use it in case you don't want anyone to see the comments you leave on the system. Example:

<%-- Nobody is gonna see this --%>

<%# ... %>

If you want to do some data binding, you have to use this tag. It can be used with DataBinder.Eval(), DataBinder.Bind() or any member which you have in the page (public or protected). The only "problem" is that you have to use these tags only in server-side elements (with runat="server"). You will find this tag being used in some controls such as GridView, DetailsView and Repeater. Example:


    
         <%# Eval("LastName") %>, <%# Eval("FirstName") %>
    

 

<%$ %>

This one is used to evaluate expressions in a configuration file. For example:

    SelectCommand="SELECT * FROM [Employees]"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString1 %>">

<%@ %>

This tag is used to specify the settings used by the page and user controls. Example:

<%@ Page Language="VB" ContentType="text/xml" %>

Did you like the article?

You can subscribe and get them as soon they are online, share using the buttons bellow or leave a comment.

And you also can share using your favorite social network: