Substitution Control ASP.NET

Introduction

This article demonstrates an interesting and very useful concept in ASP.NET.

Question: What is substitution control?

In simple terms "This enables the dynamic update of a portion of a page based on a value received from the method".

Step 1: Create a new web application

Output1.jpg

Step 2: Add a method name for the substitution control

Output2.png

Step 3: The complete code of WebForm1.aspx is as in the following:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SubstitutionControlASP.NET.WebForm1" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head id="Head1" runat="server">  
  5.     <title></title>  
  6. </head>  
  7. <body>  
  8.     <form id="form1" runat="server">  
  9.     <center>  
  10.         <div>  
  11.             <table>  
  12.                 <tr>  
  13.                     <td colspan="2" align="center">  
  14.                         <asp:Label ID="Label1" runat="server" Text="Substitution Control - ASP.NET" Font-Bold="true"  
  15.                             Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>  
  16.                     </td>  
  17.                 </tr>  
  18.                 <tr>  
  19.                     <td>  
  20.                         <asp:Label ID="Label6" runat="server" Text="Please Enter FirstNumber" Font-Size="Large"  
  21.                             Font-Names="Verdana" Font-Italic="true"></asp:Label>  
  22.                     </td>  
  23.                     <td>  
  24.                         <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>  
  25.                     </td>  
  26.                 </tr>  
  27.                 <tr>  
  28.                     <td>  
  29.                         <asp:Label ID="Label2" runat="server" Text="Please Enter SecondNumber" Font-Size="Large"  
  30.                             Font-Names="Verdana" Font-Italic="true"></asp:Label>  
  31.                     </td>  
  32.                     <td>  
  33.                         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  34.                     </td>  
  35.                 </tr>  
  36.                 <tr>  
  37.                     <td colspan="2" align="center">  
  38.                         <asp:Button ID="Button1" runat="server" Text="Addition" Font-Names="Verdana" Width="213px"  
  39.                             BackColor="Orange" Font-Bold="True" OnClick="Button1_Click" />  
  40.                     </td>  
  41.                 </tr>  
  42.                 <tr>  
  43.                     <td colspan="2" align="center">  
  44.                         <asp:Substitution ID="Substitution1" runat="server" MethodName="GetAdd" />  
  45.                     </td>  
  46.                 </tr>  
  47.                 <tr>  
  48.                     <td colspan="2" align="center">  
  49.                         <asp:Label ID="Label5" runat="server" Font-Bold="true" Font-Names="Verdana"></asp:Label>  
  50.                     </td>  
  51.                 </tr>  
  52.             </table>  
  53.         </div>  
  54.     </center>  
  55.     </form>  
  56. </body>  
  57. </html> 

Step 4: The complete code of WebForm1.aspx.cs is as in the following:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. namespace SubstitutionControlASP.NET  
  8. {  
  9.     public partial class WebForm1 : System.Web.UI.Page  
  10.     {  
  11.         protected void Page_Load(object sender, EventArgs e)  
  12.         {  
  13.         }  
  14.         protected void Button1_Click(object sender, EventArgs e)  
  15.         {  
  16.             if (string.IsNullOrEmpty(TextBox4.Text) || string.IsNullOrEmpty(TextBox1.Text))  
  17.             {  
  18.                 Label5.Text = "Please Enter Some Values";  
  19.                 Label5.ForeColor = System.Drawing.Color.Red;  
  20.             }  
  21.             else  
  22.             {  
  23.                 a = int.Parse(TextBox4.Text);  
  24.                 b = int.Parse(TextBox1.Text);  
  25.             }  
  26.         }  
  27.         static int a, b;  
  28.         public static String GetAdd(HttpContext context)  
  29.         {  
  30.             return (a + b).ToString();  
  31.         }  
  32.     }  
  33. } 

Step 5: The output of the application is as in the following:

Output3.png

Step 6: The updated data output of the application is as in the following:

Output4.png

I hope this article was useful for you.