Partager via


Embedded Code Blocks in ASP.NET Web Forms Pages

In ASP.NET Web Forms pages, the default model for adding code is to either create a code-behind class file (a code-behind page) or to write the page's code in a script block with the attribute runat="server" (a single-file page). The code you write typically interacts with controls on the page. For example, you can display information on the page from code by setting the Text (or other) properties of controls. Another possibility is to embed code directly into the page using an embedded code block.

Note

This topic applies only to ASP.NET Web Forms pages. It does not apply to pages that you create using ASP.NET MVC (Model View Controller) or ASP.NET Web Pages.

Embedded Code Blocks

An embedded code block is server code that executes during the page's render phase. The code in the block can execute programming statements and call functions in the current page class.

The following code example shows an ASP.NET Web Forms page with an embedded code block that displays the results of a loop.

<%@ Page Language="VB" %>
<html>
<body>
    <form id="form1" runat="server">
    <% For i As Integer = 0 To 5 %> 
       <% Response.Write("<br>" & i.ToString())%> 
    <% Next%>
    </form>
</body>
</html>
<%@ Page Language="C#" %>
<html>
<body>
    <form id="form1" runat="server">
    <% for(int i = 0; i < 6; i++) %> 
       <% { Response.Write("<br>" + i.ToString()); }%>
    </form>
</body>
</html>

The following code example shows an embedded code block that displays the value of a public GetTime() function inside a span element. In embedded code blocks, the syntax <% = expression %> is used to resolve an expression and return its value into the block.

<%@ Page Language="VB" %>
<script runat=server>
Protected Function GetTime() As String
    Return DateTime.Now.ToString("t")
End Function
</script>
<html>
<body>
    <form id="form1" runat="server">
       Current server time is <% =GetTime()%>.
    </form>
</body>
</html>
<%@ Page Language="C#" %>
<script runat=server>
protected String GetTime()
{
    return DateTime.Now.ToString("t");
}
</script>
<html>
<body>
    <form id="form1" runat="server">
       Current server time is <% =GetTime()%>.
    </form>
</body>
</html>

Embedded code blocks must be written in the page's default language. For example, if the page's @ Page directive contains the attribute language="VB", the page will use the Visual Basic compiler to compile code in any script block marked with runat="server" and any in-line code in <% %> delimiters.

Uses for Embedded Code Blocks

Embedded code blocks are supported in ASP.NET Web Forms pages primarily to preserve backward compatibility with older ASP technology. In general, using embedded code blocks for complex programming logic is not a best practice, because when the code is mixed on the page with markup, it can be difficult to debug and maintain. In addition, because the code is executed only during the page's render phase, you have substantially less flexibility than with code-behind or script-block code in scoping your code to the appropriate stage of page processing.

Some uses for embedded code blocks include:

  • Setting the value of a control or markup element to a value returned by a function, as illustrated in the preceding example.

  • Embedding a calculation directly into the markup or control property.

See Also

Concepts

Introduction to Programming ASP.NET Web Forms Pages

ASP.NET Web Forms Page Code Model

ASP.NET Web Forms Page Syntax Overview