Table of Contents

Tag Syntax Reference

ASP.NET Web Forms Syntax Elements

An ASP.NET Web Forms page is a declarative text file with an .aspx file name extension. In addition to static content, you can use eight distinct syntax markup elements. This section of the QuickStart reviews each of these syntax elements and provides examples demonstrating their use.

Rendering Code Syntax: <% %> and <%= %>

Code rendering blocks are denoted with <% … %> elements, allow you to custom-control content emission, and execute during the render phase of Web Forms page execution. The following example demonstrates how you can use them to loop over HTML content.

<% For I=0 To 7 %>
     <font size="<%=i%>"> Hello World! </font> <br>
<% Next %>

Declaration Code Syntax: <script runat="server">

Code declaration blocks define member variables and methods that will be compiled into the generated Page class. These blocks can be used to author page/navigation logic. The following example demonstrates how a Subtract method can be declared within a <script runat=“server”> block, and then invoked from the page.

<script language="VB" runat=server>
Function Subtract(num1 As Integer, num2 As Integer) As Integer
  Return(num1 - num2)
End Function
</script>
 
<%
  ...
  number = subtract(number, 1)
  ...
%>

ASP.NET Server Control Syntax

Custom ASP.NET server controls enable page developers to dynamically generate HTML user interface (UI) and respond to client requests. They are represented within a file using a declarative, tag-based syntax. These tags are distinguished from other tags because they contain a “runat=server” attribute. The following example demonstrates how an <asp:label runat=“server”> server control can be used within an ASP.NET page. This control corresponds to the Label class in the System.Web.UI.WebControls namespace, which is included by default.

By adding a tag with the ID “Message”, an instance of Label is created at run time:

<asp:label id="Message" font-size=24 runat="server"/>

The control can then be accessed using the same name. The following line sets the Text property of the control.

Message.Text = "Welcome to ASP.NET"

ASP.NET HTML Server Control Syntax

HTML server controls enable page developers to programmatically manipulate HTML elements within a page. An HTML server control tag is distinguished from client HTML elements by means of a “runat=server” attribute. The following example demonstrates how an HTML <span runat=server> server control can be used within an ASP.NET page.

As with other server controls, the methods and properties are accessible programmatically, as shown in the following example.

<script language="VB" runat="server">
  Sub Page_Load(sender As Object, e As EventArgs)
    Message.InnerHtml = "Welcome to ASP.NET"
  End Sub
</script>
...
<span id="Message" style="font-size:24" runat="server"/>

Data Binding Syntax: <%# %>

The data binding support built into ASP.NET enables page developers to hierarchically bind control properties to data container values. Code located within a <%# %> code block is only executed when the DataBind method of its parent control container is invoked. The following example demonstrates how to use the data binding syntax within an <asp:datalist runat=server> control.

Within the datalist, the template for one item is specified. The content of the item template is specified using a data binding expression and the Container.DataItem refers to the data source used by the datalist MyList.

<asp:datalist id="MyList" runat=server>
  <ItemTemplate>
    Here is a value: <%# Container.DataItem %>
  </ItemTemplate>
</asp:datalist>

In this case the data source of the MyList control is set programmatically, and then DataBind() is called.

Sub Page_Load(sender As Object, e As EventArgs)
  Dim items As New ArrayList()
 
  items.Add("One")
  items.Add("Two")
  items.Add("Three")
 
  MyList.DataSource = items
  MyList.DataBind()
End Sub

Object Tag Syntax: <object runat="server" />

Object tags enable page developers to declare and create instances of variables using a declarative, tag-based syntax. The following example demonstrates how the object tag can be used to create an instance of an ArrayList class.

<object id="items" class="System.Collections.ArrayList" runat="server"/>

The object will be created automatically at run time and can then be accessed through the ID “items”.

Sub Page_Load(sender As Object, e As EventArgs)
  items.Add("One")
  items.Add("Two")
  items.Add("Three")
  ...
End Sub

Server-Side Comment Syntax: <%-- Comment --%>

Server-side comments enable page developers to prevent server code (including server controls) and static content from executing or rendering. The following sample demonstrates how to block content from executing and being sent down to a client. Note that everything between <%– and –%> is filtered out and only visible in the original server file, even though it contains other ASP.NET directives.

<%--
  <asp:calendar id="MyCal" runat=server/>
    <% For I=0 To 44 %>
             Hello World <br>
    <% Next %>
--%>

Server-Side Include Syntax: <-- #Include File="Locaton.inc" -->

Server-side #Includes enable developers to insert the raw contents of a specified file anywhere within an ASP.NET page. The following sample demonstrates how to insert a custom header and footer within a page.

<!-- #Include File="Header.inc" -->
...
<!-- #Include File="Footer.inc" -->

Page Declaration

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="WebPageSeparated.aspx.vb Inherits="WebPageSeparated" %>