ASP.NET

Examples of Using Code Render Block In ASP.NET

This article contains some Examples of Using Code Render Block In ASP.NET.

The following code shows an example to find the sum of two integers.

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <%
                int a, b, sum;
                a = 67;
                b = 578;
                sum = a + b;
                %>
            <h1>Sum of <%=a %> and <% =b %> is <% =sum %>.</h1>
        </div>
    </form>
</body>
</html>

Basically, here we are using the inline expressions also to display the value of variables a, b, and sum.

Output

Examples of Using Code Render Block In ASP.NET - Sum of Two Numbers
Examples of Using Code Render Block In ASP.NET – Sum of Two Numbers

The next example shows how to display the table of factorials. As can be seen, we use the nested loops in the following code. Also, the values of the number and its factorial are displayed using inline expressions.

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table border="2">
                <tr>
                    <th>Number</th>
                    <th>Factorial</th>
                </tr>
                <%
                    int f = 1;
                    for(int i=1;i<=10;i++)
                    { %>
                    <tr>
                        <td><% =i %></td>
                        <%
                            for(int j=1;j<=i;j++)
                            {
                                f = f * j;
                            }
                            %>
                        <td><% =f %></td>
                    </tr>

                <%
                        f = 1;
                    }%>
            </table>
        </div>
    </form>
</body>
</html>

Output

Table of Factorials Using Code Render Block in ASP.NET
Table of Factorials Using Code Render Block in ASP.NET

The following example shows how to find and display all prime numbers in the given range.

<%@ 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></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h1>Prime Numbers in the Given Range
            <%
                int min_value, max_value;
                min_value = 20;
                max_value = 78;
                bool prime = true;%>
             <% =min_value %> to <% =max_value %></h1>
            <%
                for(int i=min_value;i<=max_value;i++)
                {
                    for (int j = 2; j <= i / 2; j++)
                    {
                        if (i % j == 0)
                        {
                            prime = false;
                            break;
                        }
                    }
                    if(prime)
                    { %>
                            <span style="font-size: 20px; color: #800000;"><% =i%> </span>
                          <%
                                      }
                                      prime = true;
                                  }
                              
                %>
        </div>
    </form>
</body>
</html>

Output

Prime Numbers in the Given Range Using Code Render Block in ASP.NET
Prime Numbers in the Given Range Using Code Render Block in ASP.NET

Another example of using code render block is here. The following code shows how to display Fibonacci series up to a specific term.

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h1> Fibonacci Series Up to the number 
                <% int n = 120; %>
                <% =n %>
            </h1><h2>
            <%
                int a = -1, b = 1, c;
                c = a + b;
                do
                { %>
                    <% =c%> <%
                    a=b;
                    b=c;
                    c=a+b;
                } while (c <= n);
                %> </h2>
        </div>
    </form>
</body>
</html>

Output

Program to Display Fibonacci series Up to a Specific Term in ASP.NET
Program to Display Fibonacci series Up to a Specific Term in ASP.NET

More Examples on Code Render Block

The following example shows how to generate a code using a string. Basically, we have a string consisting of alphabets, digits, and special characters. The program should display the string with alternate uppercase and lowercase letters. However, the output string should omit all digits and special characters. Further, the first character in the output string should be in upper case. For instance, if the input is anj67ty$#km, the output should be AnJtYkM.

Another example comprises of displaying a sequence using a specific number as the first one in the sequence. Also, there are two more inputs representing the gap between consecutive numbers in the sequence. For example, if the input is 2, 4, and 3, the series will be 2 6 9 13 16 20 23 27 30 and so on.


Further Reading

Examples of List Controls in ASP.NET

Parameter and ParameterCollection in ADO.NET

Database Manipulation Using DataGrid

Code Render Block in ASP.NET

ASP.NET Practice Exercise

programmingempire

Princites

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *