The ASP.NET Web Form page is a declarative text file with an extension of .aspx. In addition to static content, you can mark elements using eight different syntaxes. This section reviews these syntax elements and provides some examples of how to use them.
Syntax for rendering code: <% %> and <%= %>
Code rendering blocks are represented by <% ... %> elements, which allow you to control the rendered content and are executed during the display phase of the Web Forms page execution. The following example demonstrates how to use them to loop through the content of HTML.
<%@ Page Language="VB" %>
<html>
<body>
<% Dim I As Integer
For I = 0 To 7 %>
<font size="<%=I%>"> Hello World! </font> <br>
<%Next %>
</body>
</html>
<% ... %> is only executed, while the expression containing the equal sign (<%= ... %>) will calculate the result when the content is displayed. Therefore, <%="Hello World" %> displays the same result as the C# code <% Response.Write("Hello World"); %>.
Note that since the language requires the use of markers to terminate or separate statements (such as the semicolon ; in C#), it is important to place these markers correctly.
The C# code
<% Response.Write("Hello World"); %> requires a semicolon to terminate the statement.
<%="Hello World"; %> Error: Causes "Response.Write("Hello World";);".
<%="Hello World" %> No semicolon is required.
Syntax for declaring code: <script runat="server">
The code declaration block defines the member variables and methods that will be compiled into the Page class. These blocks can be used to build page and navigation logic. The following example demonstrates how to define the Subtract method in the <script runat="server"> block and then call it in the page.
<html>
<script language="VB" runat=server>
Function Subtract(Num1 As Integer, Num2 As Integer) As Integer
Return Num1-Num2
End Function
</script>
<body>
<%
Dim Number As Integer = 100
Do While Number > 0
Response.Write("Value: " & Number & "<br>")
Number = Subtract(Number, 1)
Loop
%>
</body>
</html>
Please note: Unlike ASP - in ASP functions must be defined in the <% %> block - all functions and global variables must be defined using the <script runat=server> tag. The function declaration in the <% %> block will prompt a syntax compilation error message.
Server Control Syntax
Customized ASP.NET server controls allow page developers to dynamically generate HTML user interfaces and respond to client requests. They are expressed in files using a declarative, markup-based syntax. These tags differ from other tags in that they contain a "runat=server" attribute. The following example demonstrates how to use the <asp:label runat="server"> server control in an ASP.NET page. This control corresponds to the Label class in the System.Web.UI.WebControls namespace.
By adding a tag with the ID "Message", a Label instance can be created at runtime:
<asp:label id="Message" font-size=24 runat="server"/>
We can use this name to access the control . The following code sets the Text property of the control.
Message.Text = "Welcome to ASP.NET"
<html>
<script language="VB" runat=server>
Sub Page_Load(Sender As Object, E As EventArgs)
Message.Text = "Welcome to ASP.NET"
End Sub
</script>
<body>
<asp:label id="Message" font-size=24 runat=server/>
</body>
</html>
HTML server control syntax
HTML server control allows developers to programmatically operate HTML elements in the page. The HTML server control tag is different from the client HTML element in that it has the "runat=server" attribute. The following example demonstrates how to use the HTML <span runat=server> server control in an ASP.NET page.
<html>
<script language="VB" runat=server>
Sub Page_Load(Sender As Object, E As EventArgs)
Message.InnerHtml = "Welcome to ASP.NET"
End Sub
</script>
<body>
<span id="Message" style="font-size:24" runat=server/>
</body>
</html>
Data binding syntax: <%# %>
ASP.NET's built-in support for data binding allows page developers to hierarchically bind control properties to data container values. The code in the <%# %> code block is only executed when the DataBind method of its parent control container is called. The following example demonstrates how to use data binding syntax in the <asp:datalist runat=server> control.
In this data list, each item is assigned a template. The content of the item template is specified using a data binding expression, and Container.DataItem points to the data source used by the MyList data list.
<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 the DataBind() method is called.
Calling the DataBind method of a control will trigger a recursive tree (starting with the control to the lower controls in the tree); the DataBinding event of each server control in the hierarchy will be raised, and the data binding expression in the control Values are calculated accordingly. Therefore, if the page's DataBind method is called, then every data binding expression in the page will be called.
<html>
<script language="VB" runat=server>
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
</script>
<body>
<asp:datalist id="MyList" runat=server>
<ItemTemplate>
Here is a value: <%# Container.DataItem %>
</ItemTemplate>
</asp:datalist>
</body>
</html>
ASP.NET 2.0 also includes a new simplified data binding syntax, which allows controls to automatically data-bind to data source controls without having to call DataBind() in the page code. This syntax is discussed in the "Performing Data Access" chapter.
Object tag syntax: <object runat="server" />
Object tag allows page developers to use declarative, tag-based syntax to declare and create variable instances. The following example demonstrates how to use object notation to create an instance of the ArrayList class.
This object is automatically created at runtime and can be accessed via the ID "items".
<html>
<script language="VB" runat=server>
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
ArrayItems.Add("One")
ArrayItems.Add("Two")
ArrayItems.Add("Three")
MyList.DataSource = ArrayItems
MyList.DataBind()
End Sub
</script>
<body>
<object id="ArrayItems" class="System.Collections.ArrayList" runat=server/>
<asp:datalist id="MyList" runat=server>
<ItemTemplate>
Here is a value: <%# Container.DataItem %>
</ItemTemplate>
</asp:datalist>
</body>
</html>
Server-side comment syntax: <%-- Comment --%>
Server-side comments allow page developers to prevent the execution and rendering of server code (including server controls) and static content. The following example demonstrates how to prevent content from being executed and sent to the client. Please note that all information between <%-- and --%> will be filtered out and will only be visible in the original server file, even if it contains other ASP.NET directives.
<html>
<body>
The below content has been hidden from browser clients using a server-side comment
(view the .aspx source to see what we mean :-)
<%--
<asp:calendar id="MyCal" runat=server/>
<% For I = 0 To 44 %>
Hello World <br>
<%Next %>
--%>
</body>
</html>
Server-side file inclusion syntax: <-- #Include File="Locaton.inc" -->
Server-side file inclusion (#Include) allows developers to insert the content of a specific file anywhere on the ASP.NET page. The following example demonstrates how to insert custom titles and footers into a page.
<html>
<body>
<!-- #Include File="Header.inc" -->
<br />
<h3> Main page content </h3>
<br />
<!-- #Include File="Footer.inc" -->
</body>
</html>
Expression syntax: <%$ ... %>New features in 2.0
ASP.NET 2.0 adds a new declarative expression syntax for value substitution before page analysis. It is very useful when we need to replace the server control property value with the connection string value or application setting in the Web.config file. It can also be used to replace values in resource files during localization.
<asp:SqlDataSource ID="SqlDataSource1" ConnectionString='<%$ connectionStrings:Pubs %>' runat="server" SelectCommand="sp_GetAuthors" />
<asp:Label ID="Label1" Text='<%$ Resources: ExchRate, ConvertLabel %>' runat="server"/>