You can use code render blocks to define inline code or expressions that will execute when a page is rendered. Code within a code render block is executed immediately when it is encountered during page rendering. On the other hand, code within a code declaration block (within <script> tags) is executed only when it is called or triggered by user or page interactions. There are two types of code render blocks—inline code, and inline expressions—both of which are typically written within the body of the ASP.NET page.
Inline code render blocks execute one or more statements, and are placed directly inside a page’s HTML between <% and %> delimiters. In our example, the following is a code render block:
Visual Basic LearningASP\VB\Hello.aspx(excerpt)
<% Dim Title As String = "This is generated by a code renderblock." %>
C# LearningASP\CS\Hello.aspx(excerpt)
<% string Title = "This is generated by a code render block."; %>
These code blocks simply declare a String variable called Title, and assign it the value This is generated by a code render block.
Inline expression render blocks can be compared to Response.Write in classic ASP. They start with <%= and end with %>, and are used to display the values of variables and the results of methods on a page. In our example, an inline expression appears immediately after our inline code block:
Visual Basic LearningASP\VB\Hello.aspx(excerpt)
<%= Title %>
C# LearningASP\CS\Hello.aspx(excerpt)
<%= Title %>
If you’re familiar with classic ASP, you’ll know what this code does: it simply outputs the value of the variable Title that we declared in the previous inline code block.
Inline code render blocks execute one or more statements, and are placed directly inside a page’s HTML between <% and %> delimiters. In our example, the following is a code render block:
Visual Basic LearningASP\VB\Hello.aspx(excerpt)
<% Dim Title As String = "This is generated by a code renderblock." %>
C# LearningASP\CS\Hello.aspx(excerpt)
<% string Title = "This is generated by a code render block."; %>
These code blocks simply declare a String variable called Title, and assign it the value This is generated by a code render block.
Inline expression render blocks can be compared to Response.Write in classic ASP. They start with <%= and end with %>, and are used to display the values of variables and the results of methods on a page. In our example, an inline expression appears immediately after our inline code block:
Visual Basic LearningASP\VB\Hello.aspx(excerpt)
<%= Title %>
C# LearningASP\CS\Hello.aspx(excerpt)
<%= Title %>
If you’re familiar with classic ASP, you’ll know what this code does: it simply outputs the value of the variable Title that we declared in the previous inline code block.