Share single Code Behind file with multiple web forms in ASP.Net using C# and VB.Net

Mohammed Imtiyaz Feb 21, 2015

Demo

Share single CodeBehind file for multiple Web Forms in ASP.Net

Let’s say you have an ASP.Net Web Application in which you have multiple Web Forms with different languages / interfaces. By default, each Web Form will have separate code file. If you make changes in one code file then you have to modify all the respective code files. This is a time consuming process. To reduce the work load and to maintain consistency in code file, we can maintain single code file for multiple Web Forms.

In this tutorial we'll take the following files:

Web Forms

  • interface_english.aspx
  • interface_arabic.aspx
  • interface_hindi.aspx
  • interface_telugu.aspx

CodeBehind File (C#)

  • interface_english.aspx.cs

We’ll take one CodeBehind file, “interface_english.aspx.cs” for multiple Web Forms

Now the question is how to inherit a single CodeBehind file for multiple Web Forms. The code below will explain you how to achieve this.

<%@ Page Language="C#" AutoEventWireup="true" Inherits="interface_english" CodeFile="interface_english.aspx.cs" %>

Description

Code File Name of the CodeBehind file (eg: interface_english.aspx.cs) you want to call
Inherits Public partial class name used in interface_english.aspx.cs code file

Web Form (Default / English Interface)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="interface_english.aspx.cs" Inherits="interface_english" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
	<title></title>
</head>
<body>
<form id="form1" runat="server">
	<p>English&nbsp;<a href="interface_arabic.aspx">Arabic</a>&nbsp;<a href="interface_hindi.aspx">Hindi</a>&nbsp;<a href="interface_telugu.aspx">Telugu</a></p>
	<h3>English Interface</h3>
	<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
  	<asp:Label ID="lblMessage" runat="server"></asp:Label>
</form>
</body>
</html>

You just need to change the "Inherits" attrubute for respective Web Forms as mentioned below

Arabic Interface

<%@ Page Language="C#" AutoEventWireup="true" Inherits="interface_arabic" CodeFile="interface_english.aspx.cs" %>

Hindi Interface

<%@ Page Language="C#" AutoEventWireup="true" Inherits="interface_hindi" CodeFile="interface_english.aspx.cs" %>

Telugu Interface

<%@ Page Language="C#" AutoEventWireup="true" Inherits="interface_telugu" CodeFile="interface_english.aspx.cs" %>

C#

This CodeBehind file is common for all Web Forms.

using System;

public partial class interface_english: System.Web.UI.Page
{
    // Common logic that all the pages should have.
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("This is a common text for all Web Forms.");
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "You are viewing English Interface.";
    }
}

public partial class interface_arabic: interface_english
{
    // Specific logic for interface_arabic
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "تقوم بعرض واجهة اللغة العربية.";
    }
}

public partial class interface_hindi: interface_english
{
    // Specific logic for interface_hindi
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "आप हिंदी इंटरफेस देख रहे हैं।";
    }
}

public partial class interface_telugu: interface_english
{
    // Specific logic for interface_telugu
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = "మీరు తెలుగు ఇంటర్ఫేస్ చూస్తున్నారు";
    }
}