Basically, script commands can be added to an .aspx file in two ways:
- as code render blocks defined within <
% ... %
> delimiters;
- as code declaration blocks defined within <
script runat="server"
> ... </script
> tags.
Code render blocks define inline code or inline expressions that are declaratively set within the document body and execute when the page is rendered at runtime.
Code render blocks are mainly used to custom-control page content generation.
When using code render blocks, the server-side instructions are differentiated from text and HTML by using delimiters. A delimiter in ASP.NET is a sequence of characters that mark the start and end of a unit.
ASP.NET supports two types of code render blocks: inline code and inline expressions.
- the symbols <
%
and %
> are used to delimit or set the bounds of <% inline code %
>.
- the symbols <
%=
and %
> are used to delimit <%= inline expressions %
>.
Within the <% inline code %
> delimiters, you can include any fragment of code ( such as statements, variables, operators, control structures such as conditionals, loops, etc. ) that is valid for the programming language set for the page.
ASP.NET executes inline code as a whole and displays the output of the code render block when the page is rendered at runtime.
Within the <%= inline expression %
> delimiters ( sometimes referred to as the output directive ), you can include any expression that is valid for the programming language set for the page.
ASP.NET uses the output directive to evaluate and display the value of an expression when the page is rendered at runtime.
The following example shows a simple .aspx page containing a code render block that defines an inline expression:
<html>
<body>
<p>Today is <%= DateTime.Now %
>.</p>
</body>
</html>
The C# expression DateTime.Now returns the current date and time from the Web server. When the server processes this page, it evaluates the expression <%= DateTime.Now %
> and returns the page to the browser with the following result:
Today is 1/10/2025 12:27:39 AM.
Show me
<%= inline expression %
> can also be useful for testing code, as it provides a simple way to check a value that you may need to use in other parts of your code.