Code Render Block in ASP.NET


Examples of Using Code Render Block:

This article describes Code Render Block in ASP.NET. Basically, a Code Render Block in ASP.NET refers to the inline code that we use in the ASPX page along with server-side and client-side tags.

What is Code Render Block?

Basically, a code render block is a block containing programming statements within a web page in ASP.NET, Since we create a web page in ASP.NET as an ASPX file, the code render block is placed in the ASPX file.

In brief, we can use two forms of code render block – inline code and inline expression. Further, a code render block is enclosed within <%….%>. The following example shows an inline code. In addition, we use <%=expression%> to display an inline expression.

As an illustration, the web page displays only the current date and time. Therefore, it requires a single statement. Hence, we use the Response.Write() statement as the inline code.

            <h1 style="text-align: center;">An Example of Inline Code (Code Render Block)</h1>
            <h2>Current Date: <%Response.Write(DateTime.Now.ToString("F")); %></h2>


Example of Inline Code
Example of Inline Code

Likewise, the following example shows an inline expression. Basically, an inline expression replaces a Response.Write() statement. Therefore, we use an inline expression, whenever we need to display a computed value.

The following example shows the implementation of for-loop as an inline code. while the value of variable sum is displayed as an inline expression.

           <h1 style="text-align: center;">An Example of Inline Expressions (Code Render Block)</h1>
            <%  int sum = 0;
                for (int i = 1; i <= 5; i++)
                    sum += i;
            <h2>Sum of first 5 integers = <%=sum %></h2>


Example of Inline Expression


As a matter of fact, the use of inline code and expressions are beneficial in many situations. For instance, suppose we create the web page that requires very less code, then we can use it for simplicity. Moreover, it is very easy to work with html tags using inline code. Also, we can display computed values easily in html elements without requiring to use code behind.


In particular, the use of code with tags makes the web page less maintainable. Therefore, if the code is sufficiently large and require many user-defined functions, we should use the code-behind file.

An Example

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="CodeRenderBlock.WebForm3" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <title>Code Render Block Example</title>
            margin: 10px;
            border: 5px outset darkred;
            font-size: 20px;
            padding: 10px;
            border: 5px outset coral;
            margin: 5px;
            margin: 100px;
    <form id="form1" runat="server">
        <div class="c3">
            <h2>Table of Sum</h2>
            <center><table class="c1">
                        <th>Sum of Integers from 1 to N</th>
                    <%for (int i = 1; i <= 10; i++)
                            int sum = 0;
                            for (int j = 1; j <= i; j++)
                                sum += j;
                        <td class="c1 c2"><%=i %></td>
                        <td class="c1 c2">
                            <%=sum %>
                    <%} %>


The following image shows the output that above code generates.

Table of Sum Using Code Render Block
Table of Sum Using Code Render Block

As shown above, we create the web page using only HTML tags. Evidently, the web page displays a table of the sum. However, we create the rows using a nested for loop. Therefore, we use inline code to implement the for- loop. Also, we use the inline expressions to display values inside the td tag.

Further Reading

Parameter and ParameterCollection in ADO.NET

Database Manipulation Using DataGrid

Example of Button and Link Button Control in ASP.NET

Example of Chart Control in ASP.NET

Creating a DataTable from a DataReader in ASP.NET

Deleting a Record using DataGrid Control in ASP.NET

Edit a Record Using DataGrid Control in ASP.NET

Insert a Record Using ItemCommand Event in DataGrid

CRUD Operations with DataGrid in ASP.NET

Creating Columns in a DataGrid Control

XML Documents and DataSet in ASP.NET

Code Render Block in ASP.NET

ASP.NET Core Features and Advantages

Display Images Using DataList Control

Adding Images Using Image Control

Creating a Group of Radio Buttons Using RadioButtonList Control

Example of Button Control in ASP.NET

Using MD5 Hash Algorithm

ItemDataBound Event in DataList

More Features of DataList in ASP.NET

A Simple Example of Using a DataList Control in ASP.NET

Properties and Methods of DataList Control in ASP.NET

ASP.NET Practice Exercise

Exploring DataList Control in ASP.NET

Custom Validator Control in ASP.NET

Validation Summary Control in ASP.NET

Validation Controls Examples – RequiredFieldValidator, CompareValidator, and RangeValidator

An Example of Data Binding with RadioButtonList Control

Binding Data to Web Control in ADO.NET

Examples of AdRotator Control in ASP.NET

Examples of Validation Controls in ASP.NET

Overview of MVC architecture in ASP.NET


You may also like...