Programmingempire
Examples of Using Code Render Block:
https://www.programmingempire.com/examples-of-using-code-render-block-in-asp-net/
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.
<div>
<h1 style="text-align: center;">An Example of Inline Code (Code Render Block)</h1>
<h2>Current Date: <%Response.Write(DateTime.Now.ToString("F")); %></h2>
</div>
Output
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.
<div>
<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>
</div>
Output
Advantages
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.
Drawbacks
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="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Code Render Block Example</title>
<style>
.c1{
padding:10px;
margin: 10px;
border: 5px outset darkred;
text-align:center;
background-color:lightcoral;
color:navy;
}
.c2{
background-color:lightpink;
color:darkmagenta;
font-size: 20px;
font-weight:bolder;
padding: 10px;
border: 5px outset coral;
margin: 5px;
}
.c3{
margin: 100px;
text-align:center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="c3">
<h2>Table of Sum</h2>
<center><table class="c1">
<thead>
<tr>
<th>N</th>
<th>Sum of Integers from 1 to N</th>
</tr>
</thead>
<tbody>
<%for (int i = 1; i <= 10; i++)
{
int sum = 0;
for (int j = 1; j <= i; j++)
{
sum += j;
}%>
<tr>
<td class="c1 c2"><%=i %></td>
<td class="c1 c2">
<%=sum %>
</td>
</tr>
<%} %>
</tbody>
</table></center>
</div>
</form>
</body>
</html>
Output
The following image shows the output that above code generates.
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
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
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
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